我有以下Timer.cpp,Timer.h和main.cpp文件。我試圖從我的main.cpp文件中的Timer.cpp文件中調用函數,並且在主函數中包含了Timer.h,但它仍然不起作用。有人可以解釋爲什麼嗎?我用C++有點生疏,覺得我犯了一個愚蠢的錯誤。預先感謝您的幫助。如何從main.cpp文件中的單獨cpp文件調用函數?
#Timer.h file
#ifndef __Notes__Timer__
#define __Notes__Timer__
#include <iostream>
class Timer {
public:
Timer();
void start();
void stop();
void clear();
float getDelta();
};
#endif
#Timer.cpp file
#include "Timer.h"
clock_t startTime;
clock_t stopTime;
Timer::Timer(){
startTime = 0;
stopTime = 0;
}//Timer
void start(){
startTime = clock();
}//start
void stop(){
stopTime = clock();
}//stop
float getDelta(){
return stopTime-startTime;
}//getDelta
#main.cpp file
#include "Timer.h"
#include <iostream>
using namespace std;
int main(){
char quit;
start();
cout << "Would you like to quit? Y or N: ";
cin >> quit;
if (quit != 'Y' || quit != 'y'){
while (quit != 'Y' || quit != 'y'){
cout << "Would you like to quit? Y or N: ";
cin >> quit;
}//while
}//if
else {
stop();
cout << getDelta();
exit(0);
}//else
}//main
你應該閱讀本:http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier – chris 2013-04-05 03:28:03
你需要一個計時器來啓動/停止/無論。 – chris 2013-04-05 03:29:29