|
Forward Declaration Problem, among other things... |
Kibiz0r
Member #6,203
September 2005
|
I got a million of these: C:\...main.cpp:37: error: invalid use of undefined type `struct Unit' For different classes though.
Also, there's a problem with my dynamic allocation of the Cell pointer array. I've never done that sort of thing before, so I have no idea what I'm doing really. Thanks. --- |
Indeterminatus
Member #737
November 2000
|
using namespace std;
This is evil. If you want to save typing std:: for every std::vector, use using std::vector; instead. For your errors: A forward declaration is no full-type declaration, and you may only perform operations that do not need a full-type (like declaring a pointer or a reference to that type). As soon as you start needing more information (like, dereferencing it, accessing a specific field etc.) about it, the compiler must already know the full type, which means that a forward declaration won't do any more. Solutions to your problem: a) Move all method definitions to a .cpp file, and include the full declarations of the types needed (like, if you have class Map; as forward declaration, #include <map.hpp>, assuming class Map is defined in map.hpp. b) Move all method definitions to a place where the classes are already fully defined. _______________________________ |
Carrus85
Member #2,633
August 2002
|
using namespace std; This is not evil, it is perfectly valid and recommended, as long as you control the scope of the statement. The basic rule of thumb is that the using namespace foo; DECLARATION IS NOT SOMETHING YOU WANT TO DO IN A HEADER FILE (if you need to do it in a header file, do it on a function by function basis. Never just leave a using namespace statement in the global scope if you can help it.) If you are using a lot of namespaced stuff in a header file, usually you enclose the using directive inside of the function scope to prevent unwanted namespace pollution. This same argument goes for even the using std::vector statement, even though the using std::vector statement is considerably less problematic. edit: using namespace std; in a cpp file is just fine as long as you don't care that it opens up hundereds of names into your current scope; you just don't want to force the entire std namespace on the user of a header file.
|
AngelChild
Member #3,401
April 2003
|
Alternatively, you can use a temporary namespace like this to hide it in a header file: #include <vector> namespace temporary { using namespace std; class Thing { vector<int> lookImAVector; }; } using temporary::Thing;
----- |
|