“gettimeofday”不能用作函数

0 c++

我在这里错过了什么,这是我的主程序,我还有一个 makefile,一切正常,错误就在这里的某个地方。

#include <iostream>
#include <observer.h>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) {
    std::fstream in;
    int gettimeofday;
    //CPUtypeandmodel
    struct timeval now;
    gettimeofday(&now, NULL);
    cout << "Status report as of : " << ctime((time_t*)&now.tv_sec) << endl;
    // Print machine name
    in.open("/proc/sys/kernel/hostname");
    string s;
    in >> s;
    cout << "Machine name: " << s << endl;
    in.close();

    return 1;
}  //end main
Run Code Online (Sandbox Code Playgroud)

当我尝试制作文件时,会发生这种情况

observer.cpp: In function ‘int main(int, char**)’:
observer.cpp:13:26: error: ‘gettimeofday’ cannot be used as a function
   gettimeofday(&now, NULL);
                          ^
<builtin>: recipe for target 'observer.o' failed
make: *** [observer.o] Error 1


                                                 
Run Code Online (Sandbox Code Playgroud)

Sha*_*ger 6

您命名了一个局部int变量gettimeofday,这会阻止您在gettimeofday()三行之后调用该函数。不要那样做。将变量命名为其他名称,或者(假设它似乎未使用)将其删除。