【发布时间】:2015-10-25 17:32:57
【问题描述】:
我正在尝试制作一个让 6 个数字随机出现的程序。
这是我的 .pro 文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Lotto
TEMPLATE = app
CONFIG += c++11
SOURCES += main.cpp\
mainwindow.cpp \
lottogenerator.cpp
HEADERS += mainwindow.h \
lottogenerator.h
FORMS += mainwindow.ui
这是我的 .h 文件
#ifndef LOTTOGENERATOR_H
#define LOTTOGENERATOR_H
#include <string>
#include <random>
#include <array>
#include <chrono>
class LottoGenerator
{
public:
typedef std::chrono::high_resolution_clock myclock;
LottoGenerator();
std::array<int, 6> get();
private:
int rand();
std::mt19937 *engine;
std::uniform_int_distribution<int> distribution;
myclock::time_point beginning = myclock::now();
};
#endif // LOTTOGENERATOR_H
这是我的 .cpp 文件。
#include "lottogenerator.h"
LottoGenerator::LottoGenerator()
: distribution(1,45)
{
myclock::duration d = myclock::now() - beginning;
unsigned int seed = d.count();
engine.seed(seed);
}
std::array<int, 6> LottoGenerator::get()
{
std::array<int, 6> numbers;
numbers[0] = rand();
numbers[1] = rand();
numbers[2] = rand();
numbers[3] = rand();
numbers[4] = rand();
numbers[5] = rand();
return numbers;
}
int LottoGenerator::rand()
{
return distribution(engine);
}
当我运行时,“C1083:无法打开包含文件:'chrono':没有这样的文件或目录”弹出。
如果您能提供帮助将不胜感激:)
【问题讨论】:
-
您使用的是哪个编译器?可能是不完全支持C++11...
-
我不使用这个编译器,但我认为你也必须设置
QMAKE_CXXFLAGS += -std=c++11。这是讨论here -
@CoryKramer 鉴于提问者使用 MSVC,您所指的标志不受支持且不必要。 MSVC 支持它所支持的任何东西,没有办法哄它支持更多。
-
您使用的是哪个版本的 MSVC?
-
我已经安装了 MSVC 2015 社区,但仍然遇到同样的问题。我应该删除 MSVC 2010 还是在 QT creator 中进行一些设置更改?
标签: c++ qt visual-c++ chrono