extern自动变量没有初始化程序 [英] extern auto variable has no initializer

查看:198
本文介绍了extern自动变量没有初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的C ++程序中使用全局时间戳(std :: chrono :: high_resolution_clock :: now())。我在头文件Header.h中声明了它:

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;

我想在main中初始化一个值,所以在main.cpp中,我做了:

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!

推荐答案

auto 应该如何推导开始

您需要声明类型

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;

这篇关于extern自动变量没有初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆