extern auto variable has no initializer

2020-04-16 19:43发布

问题:

I need to use a global timestamp (std::chrono::high_resolution_clock::now()) in my c++ program. I declared it in the header file Header.h:

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

I want to initialize a value in main, so in main.cpp, I did:

#include"Header.h"
#include<chrono>
using namespace std;
auto start;
int main(){
   start = std::chrono::high_resolution_clock::now();
}

However, when compiling it, I got:

error: declaration of ‘auto start’ has no initializer

Can anybody tell me what I did wrong? Thanks!

回答1:

How is auto supposed to deduce the type of start?
You need to declare the type

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


标签: c++ time clock