C++でオブジェクト生成時
#include "watch.hpp" int main( int argc, char** argv ) { Watch watch(); watch.start(); // なんか処理 watch.stop(); std::cout << "計算時間 : " << watch.getInterval() << " sec" << std::endl; return 0; }
↑みたいにしたら怒られた↓
g++ -c file.cpp file.cpp: In function ‘int main(int, char**)’: file.cpp:25: error: request for member ‘start’ in ‘watch’, which is of non-class type ‘Watch()’ file.cpp:27: error: request for member ‘stop’ in ‘watch’, which is of non-class type ‘Watch()’ file.cpp:30: error: request for member ‘getInterval’ in ‘watch’, which is of non-class type ‘Watch()’
を参考にすると、どうやら
Watch watch();
が関数の宣言と解釈されてるらしい。
Watch watch;
に書きなおしたら無事動いた。