无法在类中创建方法

时间:2014-12-27 13:46:10

标签: c++

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

我在我的heder文件中没有出现任何错误,但包含它只是以防万一

&#13;
&#13;
#ifndef TEST_H_
#define TEST_H_

class Test {
private:
	bool happy;

public:
	void makeHappy();
	void makeSad();
	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() {
	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;
}
This is the new error message I get when compiling

<!-- begin snippet: js hide: false -->
&#13;
&#13;
&#13;

很抱歉,如果这个问题是开放式的,但我似乎无法在其他地方找到答案。

2 个答案:

答案 0 :(得分:0)

这是你的头文件Test.h:

  1. 快乐的成员被命名为bhappy _
  2. 让私人成员考虑添加getter和setter公共成员函数
  3. #ifndef TEST_H_
    #define TEST_H_
    
    class Test {
    private:
    	bool bhappy_;
    public:
        Test() // ctor
        virtual ~Test() // dtor
    public:
    	void makeHappy();
    	void makeSad();
    	void speak();
    };
    
    #endif /* TEST_H_ */

    这是你的Test.cpp文件:

    1. 建议不要使用namespace std;
    2. 两次不要包含标题
    3. //.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;
      }
      

答案 1 :(得分:-1)

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

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

见。