2014-12-27 102 views
-5

我是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 -->

如果对不起这个问题是开放性的,但我似乎无法在别处找到答案。

+1

更改为'无效的Test ::说话(){ '与其他方法一样。 – 2014-12-27 13:49:22

+1

一般规则:修复第一个问题。通常编译器会因错误而感到困惑,如果更早的错误得到解决,进一步的错误就会消失。 – gnasher729 2014-12-27 13:55:45

+1

缺少其他评论/答案的重要事项:不要包含.cpp文件。 – Chris 2014-12-27 14:05:17

回答

0

这是你的头文件Test.h:

  1. 幸福成员被评为bhappy_
  2. 的私有成员认为增加一个getter和setter公有成员函数

#ifndef TEST_H_ 
 
#define TEST_H_ 
 

 
class Test { 
 
private: 
 
\t bool bhappy_; 
 
public: 
 
    Test() // ctor 
 
    virtual ~Test() // dtor 
 
public: 
 
\t void makeHappy(); 
 
\t void makeSad(); 
 
\t void speak(); 
 
}; 
 

 
#endif /* TEST_H_ */

这是你的Test.c pp文件:

  1. 建议不要使用namespace std;
  2. 不包括你的头两次

//.cpp file 
 
#include "Test.h" 
 
#include <iostream>//unresolved inclusion 
 

 
//ctor 
 
Test::Test() : bhappy_(false) 
 
{} 
 

 
Test::~Test(){} 
 

 
void Test::speak() { 
 

 
    if (bhappy_) {//Symbol hapy could not be resolved 
 
     std::cout << "Meouw!" << endl; 
 
    } else { 
 
     std::cout << "Ssssss!" << endl; 
 
    } 
 
} 
 

 
void Test::makeHappy() { 
 
    bhappy_ = true;//Symbol hapy could not be resolved 
 
} 
 

 
void Test::makeSad() { // member decleration not found 
 
    bhappy_ = false;//Symbol hapy could not be resolved 
 
}

这是你的主要功能:

#include "Test.h" 

int main(int argc, char** argv) { 
    Test jim; 
    jim.makeHappy();//method make happy could not be resolved 
    jim.speak();//method speak could not be resolved 

    Test bob; 
    bob.makeSad();//method make happy could not be resolved 
    bob.speak();//method speak could not be resolved 

    return 0; 
} 
+0

修复了错误,但仍然显示符号bhappy_无法解析。在“Test :: Test():bhappy_(false)”我得到的消息“成员高兴没有在这个构造函数中初始化,如果我不包括”使用命名空间标准“我得到一个错误消息说,符号cout和endl无法解决。 – hm3293 2014-12-28 00:02:56

+0

看看新的编辑它应该修复所有的编译错误。祝你好运 – 2014-12-28 00:06:45

+0

我现在得到这个错误:no'void Test :: makeHappy()'成员函数在'Test'类中声明。另外为什么不建议使用名称空间标准。我可能只是卸载并重新安装我的编译器,如果它可能是一个问题。感谢所有的帮助! – hm3293 2014-12-28 00:30:14

-1

您需要指定Test :: speak()。 应该使用这个。使用类属性。想想getter和setter。

最后,看看C++培训,应该是有用的^^。

再见。