2

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!

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
mmirror
  • 175
  • 1
  • 3
  • 10
  • Possible duplicate of [Can global auto variables be declared in h files?](https://stackoverflow.com/questions/57805451/can-global-auto-variables-be-declared-in-h-files) – ead Sep 05 '19 at 15:11
  • the newer Q&A has better answers (and clearer question IMO). – ead Sep 05 '19 at 15:11

1 Answers1

5

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;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Thanks! But I'm getting another error saying: "invalid use of template-name ‘std::chrono::time_point’ without an argument list", but I've checked the c++11 manual which says that std::chrono::time_point is the return of high_resolution_clock. So what does this error mean? – mmirror Jun 08 '16 at 11:47