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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3495449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:58:30  来源:igfitidea点击:

C++ "Variable not declared in this scope" - again

c++variablesscopeprotectedout

提问by Jubbsee

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.

我想这是一个非常简单的问题,并且可能已经被多次回答了。但是,我真的很擅长 C++,并且搜索了无济于事的解决方案。我真的很感激你的帮助。

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.

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

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".

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

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

我不知道还能搜索什么。提前致谢。

回答by Bob Fincheimer

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 thisparameter. Without Animal::it thinks you are just creating a new global function called setName

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

回答by Timo Geusch

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

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

You'll have to change setName to this:

您必须将 setName 更改为:

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

回答by AnT

"It compiles if I comment out the setName method"

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

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_nameformat. That would be Animal::setNamein your case.

您的程序中没有“setName 方法”(指有问题的定义)。您定义了一个名为 的完全独立的全局函数setName,它不是任何事物的“方法”。如果你想定义一个方法,即一个类的成员函数,你必须使用class_name::method_name格式来引用它。那就是Animal::setName你的情况。

回答by Douglas Leeder

The hint was "non-member function".

提示是“非成员函数”。

You need to make the function into a member function:

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

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