Getting warning in MinGW due to unordered initialized members.
Here is Aircraft.cpp/hpp as an example:
$ make
Windows makefile running...
Compiling...
g++ -Wall -std=c++11 -c -s Aircraft.cpp -o ../Temp/Aircraft.o
In file included from Aircraft.cpp:1:0:
Aircraft.hpp: In constructor 'Aircraft::Aircraft(Aircraft::Type, const TextureHolder&, const FontHolder&)':
Aircraft.hpp:76:12: warning: 'Aircraft::mSpawnedPickup' will be initialized after [-Wreorder]
Aircraft.hpp:67:17: warning: 'sf::Sprite Aircraft::mSprite' [-Wreorder]
Aircraft.cpp:23:1: warning: when initialized here [-Wreorder]
In file included from Aircraft.cpp:1:0:
Aircraft.hpp:89:12: warning: 'Aircraft::mIdentifier' will be initialized after [-Wreorder]
Aircraft.hpp:77:12: warning: 'bool Aircraft::mPickupsEnabled' [-Wreorder]
Aircraft.cpp:23:1: warning: when initialized here [-Wreorder]
Following files are affected:
Aircraft.cpp
GameServer.cpp
Player.cpp
Also there is one more warning to get rid of:
Utility.cpp: In function 'std::string toString(sf::Keyboard::Key)':
Utility.cpp:28:9: warning: enumeration value 'KeyCount' not handled in switch [-Wswitch]
Change from:
BOOK_KEYTOSTRING_CASE(F13)
BOOK_KEYTOSTRING_CASE(F14)
BOOK_KEYTOSTRING_CASE(F15)
BOOK_KEYTOSTRING_CASE(Pause)
}
return "";
}
Change this to:
BOOK_KEYTOSTRING_CASE(F13)
BOOK_KEYTOSTRING_CASE(F14)
BOOK_KEYTOSTRING_CASE(F15)
BOOK_KEYTOSTRING_CASE(Pause)
default:
return "";
}
return "";
}
Please update this source code on github. I know it is only warnings but I find them annoying.
There are also other warnings that are not justified:
template <typename Resource, typename Identifier>
void ResourceHolder<Resource, Identifier>::insertResource(Identifier id, std::unique_ptr<Resource> resource)
{
// Insert and check success
auto inserted = mResourceMap.insert(std::make_pair(id, std::move(resource)));
assert(inserted.second);
}
warning: variable 'inserted' set but not used [-Wunused-but-set-variable]
This is a flaw in g++. They should rather implement assert in a way such that with NDEBUG defined, the expression has no effect, but counts as "used".
Fixing the warnings requires useless default: labels in all of these switch statements, which is not only annoying, but makes code less readable and differ from the snippets in the book for no obvious reason. I really don't like complicating perfectly valid C++ code.
I kindly disagree here. ;)
You can get undefined behaviour, or «simply» vicious bugs when the default case is missing. Here is a dummy example where the RIGHT case is missing. Pretty painful to debug.
switch (direction) {
case UP: x = 1; break;
case DOWN: x = 2; break;
case LEFT: x = 3; break;
}
Or if you need to return some value, it's best (IMO) to use `default: return x;` than returning x after the switch:
switch (y) {
default: return x;
case A: return a;
...
}
Regarding your assertion, wouldn't it be the same to `assert(resource)` before the insertion? I fail to understand why you want to test it after..