C++ 类没有命名的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17482057/
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
Class has no member named
提问by Rapture686
I have a problem with accessing a function from a class with the class object in my main
function. I am just trying to make the object for the class and use that object to access the function inside that class's .cpp
file. I keep getting an error and I even made the simplest program to test it and I still get an error.
我在使用函数中的类对象访问类中的函数时遇到问题main
。我只是想为类创建对象并使用该对象访问该类.cpp
文件中的函数。我不断收到错误,我什至制作了最简单的程序来测试它,但仍然出现错误。
Main:
主要的:
#include <iostream>
#include "Attack.h"
using namespace std;
int main()
{
Attack attackObj;
attackObj.printShiz();
}
Class header:
类标题:
#ifndef ATTACK_H
#define ATTACK_H
class Attack
{
public:
Attack();
void printShiz();
protected:
private:
};
#endif // ATTACK_H
Class .cpp:
.cpp 类:
#include <iostream>
#include "Attack.h"
using namespace std;
Attack::Attack() {
}
void Attack::printShiz() {
cout << "Test" << endl;
}
How do I fix this error? Everytime I try to access the printShiz()
function in the Attack
class by using an object in my main
function, I get an error and it doesn't think this function exists within this class.
我该如何解决这个错误?每次我尝试访问printShiz()
的功能Attack
在我的使用对象类main
函数,我得到一个错误,它并不认为这类中这个功能存在。
Error:
错误:
error: 'class Attack' has no member named 'printShiz'
错误:“class Attack”没有名为“printShiz”的成员
回答by asde
I had a similar problem. It turned out, I was including an old header file of the same name from an old folder. I deleted the old file changed the #include directive to point to my new file and all was good.
我有一个类似的问题。事实证明,我从旧文件夹中包含了一个同名的旧头文件。我删除了旧文件,将 #include 指令更改为指向我的新文件,一切都很好。
回答by Kemin Zhou
Most of the time, the problem is due to some error on the human side. In my case, I was using some classes whose names are similar. I have added the empty() method under one class; however, my code was calling the empty() method from another class. At that moment, the mind was stuck. I was running make clean, and remake thinking that it was some older version of the header got used. After walking away for a moment, I found that problem right away. We programmers tends to blame others first. Maybe we should insist on ourselves to be wrong first.
大多数情况下,问题是由于人为方面的一些错误造成的。就我而言,我使用了一些名称相似的类。我在一个类下添加了 empty() 方法;但是,我的代码是从另一个类调用 empty() 方法。那一瞬间,脑子里一片空白。我正在运行 make clean,然后 remake 认为这是使用了一些旧版本的标头。走了一会儿,我马上发现了这个问题。我们程序员往往会先责怪别人。或许我们应该坚持自己先错。
Sometimes, I forget to write the latest update to disk and looking at the correct version of the code, but the compiler is seeing the wrong version of the code. This situation may be less a issue on IDE (I use vi to do coding).
有时,我忘记将最新更新写入磁盘并查看正确版本的代码,但编译器看到的是错误版本的代码。这种情况在 IDE 上可能不是问题(我使用 vi 进行编码)。
回答by Mufassir Chowdhury
Maybe I am 6.5 years late. But I'm answering because others maybe searching still now. I faced the same problem and was searching everywhere. But then I realized I had written my code in an empty file. If you create a project and write your code in there, there won't be this error.
也许我迟到了 6.5 年。但我回答是因为其他人现在可能还在搜索。我遇到了同样的问题,到处寻找。但后来我意识到我已经在一个空文件中编写了我的代码。如果您创建一个项目并在其中编写代码,则不会出现此错误。
回答by Norw?
Did you remember to include the closing brace in main?
您是否记得在 main 中包含右大括号?
#include <iostream>
#include "Attack.h"
using namespace std;
int main()
{
Attack attackObj;
attackObj.printShiz();
}
回答by Stefano Falasca
Try to define the functions right into the header
尝试将函数定义到标题中
#ifndef ATTACK_H
#define ATTACK_H
class Attack {
public:
Attack(){};
void printShiz(){};
protected:
private: };
#endif // ATTACK_H
and to compile. If the compiler doesn't complain about duplicate definitions it means you forgot to compile the Class.cpp file, then you simply need to do it (add it to your Makefile/project/solution... which toolchain are you using?)
并编译。如果编译器没有抱怨重复定义,这意味着您忘记编译 Class.cpp 文件,那么您只需要这样做(将其添加到您的 Makefile/项目/解决方案...您使用的是哪个工具链?)
回答by user3255112
I know this is a year old but I just came across it with the same problem. My problem was that I didn't have a constructor in my implementation file. I think the problem here could be the comment marks at the end of the header file after the #endif...
我知道这是一岁了,但我刚刚遇到了同样的问题。我的问题是我的实现文件中没有构造函数。我认为这里的问题可能是#endif 之后头文件末尾的注释标记...
回答by vesperto
Do you have a typo in your .h? I once came across this error when i had the method properly called in my main, but with a typo in the .h/.cpp (a "g" vs a "q" in the method name, which made it kinda difficult to spot). It falls under the "copy/paste error" category.
你的 .h 有错别字吗?当我在主程序中正确调用方法时,我曾经遇到过这个错误,但在 .h/.cpp 中有一个错字(方法名称中的“g”与“q”,这使得它有点难以发现)。它属于“复制/粘贴错误”类别。