我需要在我的C ++程序中使用全局时间戳(std :: chrono :: high_resolution_clock :: now())。我在头文件Header.h中声明了它:
#include<chrono>
using namespace std;
extern auto start;
Run Code Online (Sandbox Code Playgroud)
我想在main中初始化一个值,所以在main.cpp中,我做到了:
#include"Header.h"
#include<chrono>
using namespace std;
auto start;
int main(){
start = std::chrono::high_resolution_clock::now();
}
Run Code Online (Sandbox Code Playgroud)
但是,在编译时,我得到:
error: declaration of ‘auto start’ has no initializer
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?谢谢!
auto应该如何推断类型start?
您需要声明类型
extern std::chrono::high_resolution_clock::time_point start;
Run Code Online (Sandbox Code Playgroud)