这里我遗漏了什么,这是我的主程序,我还有一个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当我尝试创建该文件时,就会发生这种情况
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发布于 2020-09-25 03:02:59
您将一个本地int变量命名为gettimeofday,这将防止您在三行之后调用函数gettimeofday()。别干那事。将该变量命名为其他名称,或者(考虑到它似乎未使用)直接删除它。
发布于 2020-09-25 03:28:56
您的int gettimeofday;正在隐藏具有相同名称的函数。您甚至不需要该变量,因此请删除它。
您需要在使用的函数和类中包含ctime和sys/time.h。
您打开文件/proc/sys/kernel/hostname进行写入,除非您以root身份运行该程序,否则该操作将失败。
转换为time_t*不是必需的,因为&now.tv_sec已经是一个time_t*。
#include <sys/time.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
int main() {
// CPUtypeandmodel
timeval now;
if(gettimeofday(&now, nullptr) == 0) // check for success
std::cout << "Status report as of : " << std::ctime(&now.tv_sec) << '\n';
// Print machine name
if(std::ifstream in("/proc/sys/kernel/hostname"); in) { // open for reading
std::string s;
if(in >> s) // check for success
std::cout << "Machine name: " << s << '\n';
} // no need to call in.close(), it'll close automatically here
return 1; // This usually signals failure. Return 0 instead.
} // end mainhttps://stackoverflow.com/questions/64052773
复制相似问题