【问题标题】:extern auto variable has no initializerextern auto 变量没有初始化器
【发布时间】:2016-10-08 15:39:15
【问题描述】:

我需要在我的 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

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

【问题讨论】:

标签: c++ time clock


【解决方案1】:

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

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

【讨论】:

  • 谢谢!但是我收到另一个错误消息:“在没有参数列表的情况下无效使用模板名称'std::chrono::time_point'”,但我查看了 c++11 手册,该手册说 std::chrono: :time_point 是 high_resolution_clock 的返回值。那么这个错误是什么意思呢?
猜你喜欢
  • 2012-03-23
  • 2016-10-03
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
相关资源
最近更新 更多