C++ “变量未在此范围内声明” - 再次

发布于 2024-09-15 06:45:01 字数 1558 浏览 14 评论 0原文

我想这是一个非常简单的问题,而且可能已经被回答过多次了。然而,我对 C++ 确实很烂,并且没有找到解决方案。 我非常感谢您的帮助。

基本上:

#ifndef ANIMAL_H
#define ANIMAL_H

class Animal 
{
 public:
  void execute();
  void setName(char*);
  Animal();
  virtual ~Animal(); 

 private:
  void eat();
  virtual void sleep() = 0;

 protected:
  char* name;
};

class Lion: public Animal 
{
 public:
  Lion();

 private:
  virtual void sleep();
};



class Pig: public Animal 
{
 public:
  Pig();

 private:
  virtual void sleep();
};



class Cow: public Animal
{
 public:
  Cow();

 private:

  virtual void sleep();
};

#endif

是头文件,其中:

#include <iostream>
#include "Animal.h"

using namespace std;

Animal::Animal()
{
 name = new char[20];
}
Animal::~Animal()
{
 delete [] name;
}

void setName( char* _name )
{
 name = _name;
}

void Animal::eat() 
{
 cout << name << ": eats food" << endl;
}
void Animal::execute() 
{
 eat();
 sleep();
}


Lion::Lion()
{
 name = new char[20];
}  
void Lion::sleep()
{
 cout << "Lion: sleeps tonight" << endl;
}


Pig::Pig()
{
 name = new char[20];
}   
void Pig::sleep()
{
 cout << "Pig: sleeps anytime, anywhere" << endl;
}


Cow::Cow()
{
 name = new char[20];
}
void Cow::sleep()
{
 cout << "Cow: sleeps when not eating" << endl;
}

是C 文件。 正如您所看到的,非常简单的东西,但是,每当我尝试编译时,我都会收到:“错误:'name'未在此范围内声明”。

如果我注释掉 setName 方法,它就会编译。 Iv 尝试将“名称”设置为公开,但仍然遇到相同的错误。我还尝试在 setName() 中使用“this->name = _name”,这会导致“在非成员函数中无效使用‘this’”。

我不知道还要寻找什么。提前致谢。

I guess this is a really simple question and, probably, one that has been answered several times over. However, I really do suck at C++ and have searched to no avail for a solution.
I would really appreciate the help.

Basically:

#ifndef ANIMAL_H
#define ANIMAL_H

class Animal 
{
 public:
  void execute();
  void setName(char*);
  Animal();
  virtual ~Animal(); 

 private:
  void eat();
  virtual void sleep() = 0;

 protected:
  char* name;
};

class Lion: public Animal 
{
 public:
  Lion();

 private:
  virtual void sleep();
};



class Pig: public Animal 
{
 public:
  Pig();

 private:
  virtual void sleep();
};



class Cow: public Animal
{
 public:
  Cow();

 private:

  virtual void sleep();
};

#endif

Is the header file, where:

#include <iostream>
#include "Animal.h"

using namespace std;

Animal::Animal()
{
 name = new char[20];
}
Animal::~Animal()
{
 delete [] name;
}

void setName( char* _name )
{
 name = _name;
}

void Animal::eat() 
{
 cout << name << ": eats food" << endl;
}
void Animal::execute() 
{
 eat();
 sleep();
}


Lion::Lion()
{
 name = new char[20];
}  
void Lion::sleep()
{
 cout << "Lion: sleeps tonight" << endl;
}


Pig::Pig()
{
 name = new char[20];
}   
void Pig::sleep()
{
 cout << "Pig: sleeps anytime, anywhere" << endl;
}


Cow::Cow()
{
 name = new char[20];
}
void Cow::sleep()
{
 cout << "Cow: sleeps when not eating" << endl;
}

is the C file.
As you can see, really simple stuff, but, I get the: "error: ‘name’ was not declared in this scope" whenever I try to compile.

It compiles if I comment out the setName method. Iv tried setting 'name' to public and still get the same error. I have also tried using "this->name = _name" in setName(), which results in "invalid use of ‘this’ in non-member function".

I don't know what else to search for. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和影子一齐双人舞 2024-09-22 06:45:01
void setName( char* _name )
{
 name = _name;
}

应该是

void Animal::setName( char* _name )
{
  this->name = _name;
}

如果使用this参数,则需要有Animal::。如果没有 Animal::,它会认为您只是创建一个名为 setName 的新全局函数

void setName( char* _name )
{
 name = _name;
}

should be

void Animal::setName( char* _name )
{
  this->name = _name;
}

You need to have Animal:: if you use the this parameter. Without Animal:: it thinks you are just creating a new global function called setName

那伤。 2024-09-22 06:45:01

您编写代码 setName 的方式是一个自由函数,而不是成员函数。因此,编译器无法解析 name

您必须将 setName 更改为:

void Animal::setName( char* _name )
{
  name = _name;
}

The way you have written the code setName is a free function, not a member function. For this reason the compiler can't resolve name.

You'll have to change setName to this:

void Animal::setName( char* _name )
{
  name = _name;
}
落日海湾 2024-09-22 06:45:01

提示是“非成员函数”。

您需要将该函数变成成员函数:

void Animal::setName( char* _name )
{
 name = _name;
}

The hint was "non-member function".

You need to make the function into a member function:

void Animal::setName( char* _name )
{
 name = _name;
}
野侃 2024-09-22 06:45:01

“如果我注释掉 setName 方法,它就会编译”

您的程序中没有“setName 方法”(指有问题的定义)。您定义了一个完全独立的全局函数,名为 setName,它不是任何东西的“方法”。如果要定义一个方法,即类的成员函数,则必须使用class_name::method_name 格式引用它。在您的情况下,这将是 Animal::setName

"It compiles if I comment out the setName method"

You don't have a "setName method" in your program (referring to the problematic definition). You defined a completely independent global function called setName, which is not a "method" of anything. If you want to define a method, i.e. a member function of a class, you have to refer to it using the class_name::method_name format. That would be Animal::setName in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文