好吧,我來自Java和Python,請耐心等待。我一直在網上搜索,試圖學習如何在C++中使用頭文件,而且我還沒有做好,直到我定義了一個類。這是我的代碼。如何實現.h文件中定義的類C++
的notAClock.h
#ifndef NOTACLOCK_H_
#define NOTACLOCK_H_
namespace thenewboston {
class notAClock {
public:
notAClock();
virtual ~notAClock();
int isAClock();
};
} /* namespace thenewboston */
#endif /* NOTACLOCK_H_ */
的notAClock.cpp
/*
* notAClock.cpp
*
* Created on: Dec 22, 2012
* Author: pipsqueaker
*/
#include "notAClock.h"
namespace thenewboston {
notAClock::notAClock() {
// TODO Auto-generated constructor stub
}
notAClock::~notAClock() {
// TODO Auto-generated destructor stub
}
int notAClock::isAClock() {
return 0;
}
} /* namespace thenewboston */
,最後,我的主文件
#include <iostream>
#include "notAClock.h"
using namespace std;
int main() {
cout << "program works" << endl;
notAClock play;
}
當Eclipse嘗試編譯這對我來說(我使用CDT插件)會引發錯誤,其中的相關部分是
../src/main.cpp:13: error: 'notAClock' was not declared in this scope
../src/main.cpp:13: error: expected `;' before 'play'
make: *** [src/main.o] Error 1
我能從中得到的最多就是notAClock在主類中是未定義的。我究竟做錯了什麼?
-pipsqueaker117
你把它放在一個名字空間中,所以它是'newboston :: notAClock'。 –
你已經做了很多工作來把這個類放在一個單獨的命名空間中。不要驚訝它現在隱藏在那裏:-) – Mat