読者です 読者をやめる 読者になる 読者になる
月 の 上

C++でオブジェクト生成時の ”which is of non-class type” なんとか

C++

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()’


C++のrequest for member ‘func’ in ‘val’, which is of non-class type ‘class ()()’的なエラー - opamp_sando's blog

を参考にすると、どうやら

Watch watch();

が関数の宣言と解釈されてるらしい。

Watch watch;

に書きなおしたら無事動いた。