Sign in to follow this  

Help with this error message with vectors

This topic is 4745 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
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

This topic is 4745 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this