Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Help with this error message with vectors

This topic is 5086 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
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:
// stuff
const int right = 0;
const int left = 1;
// stuff

error handling library:
// stuff
const char* right = "No Error";
const char* wrong = "Uhoh!";
// stuff

your 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!