Help with this error message with vectors

Started by
2 comments, last by Enigma 19 years, 4 months ago
I get this error when I compile. c:\Programming\Code\Work\TitanEngine\3ds.h(94): error C2143: syntax error : missing ';' before '<' It has to do this code

vector<tMaterialInfo> pMaterials;

I have included vector header...
Advertisement
std::vector<tMaterialInfo> pMaterials;

Or use a using namespace std; declaration.

Enigma
Thanks that did the trick.
I guess I really ought to explain name lookup rather than just give you the answer.

Namespaces provide a way to partition the global namespace to avoid running into the problem of the same name being used several times in a library. A nifty little contrived example should demonstrate this problem
navigation library:// stuffconst int right = 0;const int left = 1;// stufferror handling library:// stuffconst char* right = "No Error";const char* wrong = "Uhoh!";// stuffyour project:#include "navigation.h"#include "errorhandler.h"int main(){	if (checkError(navigate(right)) == right)	{		// stuff	}}

Your compiler has absolutely no way of knowing which right you mean when. Because of this ambiguity it won't even let you get that far. Merely trying to include both headers will result in a compiler error.

The solution is to wrap each library in its own namespace, so the names are actually different:
navigation library:namespace nav{	// stuff	const int right = 0;	const int left = 1;	// stuff}error handling library:namespace errHandler{	// stuff	const char* right = "No Error";	const char* wrong = "Uhoh!";	// stuff}your project:#include "navigation.h"#include "errorhandler.h"int main(){	if (checkError(navigate(nav::right)) == errHandler::right)	{		// stuff	}}

Now the names are no longer ambiguous. nav::right refers unambiguously to the integer right from the navigation library while errHandler::right unambiguously refers to the const char* right from the error handling library.

Writing errHandler:: repeatedly can get pretty old fast, so a using declaration imports all the names in a namespace into the global namespace:
your project:#include "navigation.h"#include "errorhandler.h"int main(){	using namespace errHandler;	if (checkError(navigate(nav::right)) == right)	{		// stuff	}}

Now the unqualified right unambigously refers to right from the error handling library while the nav::right still unambiguously refers to the right from the navigation library.

Of course, if you did:
your project:#include "navigation.h"#include "errorhandler.h"int main(){	using namespace errHandler;	using namespace nav;	if (checkError(navigate(right)) == right)	{		// stuff	}}

You'd be right back where you started, which is why you shouldn't rely on using declarations too much.

The standard library functions and variables are all defined in the namespace std so that you don't have to worry about conflicting with them. If they weren't imagine what would happen if you added some 3d graphics to your project:
class vector // oops, the name vector is already used by the standard library!{	// stuff};


Enigma

This topic is closed to new replies.

Advertisement