我需要在我的C++程序中使用全局时间戳(std::chrono::high_resolution_clock::now())。我在头文件Header.h中声明了它:

#include<chrono>
using namespace std;
extern auto start;

我想在main中初始化一个值,所以在main.cpp中,我做到了:
#include"Header.h"
#include<chrono>
using namespace std;
auto start;
int main(){
   start = std::chrono::high_resolution_clock::now();
}

但是,在编译时,我得到:
error: declaration of ‘auto start’ has no initializer

谁能告诉我我做错了什么?谢谢!

最佳答案

auto应该如何推断start的类型?
您需要声明类型

extern std::chrono::high_resolution_clock::time_point start;

关于c++ - extern自动变量没有初始化程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37701323/

10-17 03:11