C++ “变量未在此范围内声明” - 再次
我想这是一个非常简单的问题,而且可能已经被回答过多次了。然而,我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该是
如果使用
this
参数,则需要有Animal::
。如果没有Animal::
,它会认为您只是创建一个名为setName
的新全局函数should be
You need to have
Animal::
if you use thethis
parameter. WithoutAnimal::
it thinks you are just creating a new global function calledsetName
您编写代码
setName
的方式是一个自由函数,而不是成员函数。因此,编译器无法解析name
。您必须将 setName 更改为:
The way you have written the code
setName
is a free function, not a member function. For this reason the compiler can't resolvename
.You'll have to change setName to this:
提示是“非成员函数”。
您需要将该函数变成成员函数:
The hint was "non-member function".
You need to make the function into a member function:
“如果我注释掉 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 theclass_name::method_name
format. That would beAnimal::setName
in your case.