【发布时间】:2018-02-11 08:33:05
【问题描述】:
我正在阅读 C++ 如何编程 并尝试使用我目前所学的知识(哈!)并编写了这个程序。 (这是我自己的事情,不是书中的练习。)输出是我想要的,但我无法修复警告。
我在 Game.h 中对 random 的使用是基于我在书中看到的。
如果我将导致警告的行放在main() 中,错误就会消失,但编译器会抛出一个致命错误,因为它不再可以访问变量engine。我明白了。
我感到很沮丧,因为我以为我在学习 C++,但 C++11 的东西似乎很快就让我忘记了。
游戏.h
#include <iostream>
#include <array>
#include <ctime>
#include <random>
std::default_random_engine engine( static_cast< unsigned int >( time(0) ) );
std::uniform_int_distribution< int > randomInt( 0, 23 );
void initPieces( std::array< int, 24 >& );
void showPieces( std::array< int, 24 > );
main.cpp
#include "Game.h"
int main() {
std::array< int, 24 > piecesPlayer1 = {};
std::array< int, 24 > piecesPlayer2 = {};
initPieces ( piecesPlayer1 );
initPieces ( piecesPlayer2 );
}
void initPieces( std::array< int, 24 >& myPieces) {
for ( unsigned int i = 0; i < 24; i += 3 ) {
myPieces[ i ] = 1;
myPieces[ i + 1 ] = 2;
myPieces[ i + 2 ] = 3;
}
for ( unsigned int i = 0; i < 24; i++ ) {
int s = randomInt( engine );
std::swap ( myPieces[ i ], myPieces[ static_cast<unsigned int>( s ) ] );
}
showPieces ( myPieces );
}
void showPieces( std::array< int, 24 > myPieces) {
for ( unsigned int i = 0; i < 24; i++ ) {
std::cout << myPieces[ i ] << " ";
}
std::cout << std::endl;
}
编辑:我忘了包括警告。
rm -fr build/*
clang++ -std=c++11 -stdlib=libc++ -Weverything -Wno-c++98-compat src/main.cpp -o build/main -Isrc/
In file included from src/main.cpp:1:
src/Game.h:7:28: warning: no previous extern declaration for non-static variable 'engine' [-Wmissing-variable-declarations]
std::default_random_engine engine( static_cast< unsigned int >( time(0) ) );
^
src/Game.h:7:28: warning: declaration requires a global constructor [-Wglobal-constructors]
std::default_random_engine engine( static_cast< unsigned int >( time(0) ) );
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/Game.h:8:38: warning: no previous extern declaration for non-static variable 'randomInt' [-Wmissing-variable-declarations]
std::uniform_int_distribution< int > randomInt( 0, 23 );
^
src/Game.h:8:38: warning: declaration requires a global constructor [-Wglobal-constructors]
std::uniform_int_distribution< int > randomInt( 0, 23 );
^~~~~~~~~~~~~~~~~~
4 warnings generated.
./build/main
2 3 2 3 3 2 2 2 2 1 1 3 2 1 3 1 1 1 3 3 3 1 1 2
2 3 3 1 2 3 3 2 1 2 1 2 1 1 3 2 3 1 3 2 3 1 2 1
【问题讨论】:
-
警告是什么?
-
@NathanOliver 是的。我忘了包括他们。我已经编辑了它们。
-
我的猜?因为您在头文件中定义了变量。头文件几乎不应该定义变量,只声明它们。想想如果您尝试在多个源文件中包含相同的头文件会发生什么。然后该变量将在多个不允许的地方定义。对于您这样一个简单的程序,您实际上并不需要头文件。只有在多个源文件中需要变量或函数声明或结构时,它才有用。
-
除非你真的、真的知道你在做什么,否则不要在 clang 中使用
-Weverything。 -
使用
-Wall而不是-Weverything。除非您像我一样是强迫症,否则请使用-Weverything并准备好进行大量代码润色......因为它很有趣!