Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern auto variable has no initializer

Tags:

c++

time

clock

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!

like image 746
mmirror Avatar asked Sep 13 '25 05:09

mmirror


1 Answers

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;
like image 128
Cory Kramer Avatar answered Sep 15 '25 17:09

Cory Kramer