我是C++的新手,但对java有一些经验。我想创建一个类,但是当我试图使在类的新方法,我得到几个错误(这是我的.cpp文件)无法在类中创建方法
//.cpp file
#include "Test.h"
#include "Test.h"
#include <iostream>//unresolved inclusion
using namespace std;
void speak() {
if (happy) {//Symbol hapy could not be resolved
cout << "Meouw!" << endl;
} else {
cout << "Ssssss!" << endl;
}
}
void Test::makeHappy() { // member decleration not found
happy = true;//Symbol hapy could not be resolved
}
void Test::makeSad() { // member decleration not found
happy = false;//Symbol hapy could not be resolved
}
我没有在我的赫德文件得到任何错误,但已包括以防万一
#ifndef TEST_H_
#define TEST_H_
class Test {
private:
\t bool happy;
public:
\t void makeHappy();
\t void makeSad();
\t void speak();
};
#endif /* TEST_H_ */
Finally I have another .cpp file I use which also gets errors
#include <iostream>//unresolved inclusion
#include "Test.h"
#include "Test.cpp"
using namespace std;
int main() {
\t Test jim;
\t jim.makeHappy();//method make happy could not be resolved
\t jim.speak();//method speak could not be resolved
\t Test bob;
\t bob.makeSad();//method make happy could not be resolved
\t bob.speak();//method speak could not be resolved
\t return 0;
}
This is the new error message I get when compiling
<!-- begin snippet: js hide: false -->
如果对不起这个问题是开放性的,但我似乎无法在别处找到答案。
更改为'无效的Test ::说话(){ '与其他方法一样。 – 2014-12-27 13:49:22
一般规则:修复第一个问题。通常编译器会因错误而感到困惑,如果更早的错误得到解决,进一步的错误就会消失。 – gnasher729 2014-12-27 13:55:45
缺少其他评论/答案的重要事项:不要包含.cpp文件。 – Chris 2014-12-27 14:05:17