STL problem

Started by
10 comments, last by Marty666 21 years, 1 month ago
I've got a problem using ... I't won't work and I can't figure out what's wrong. C++ just gives these bugs: error C2143: syntax error : missing ';' before '<' error C2501: 'vector' : missing storage-class or type specifiers error C2059: syntax error : '<' error C2238: unexpected token(s) preceding ';' vectortest.exe - 4 error(s), 0 warning(s) Here is a very simple program I wrote to test it: #include <vector> struct mystruct { int Value1, Value2; }mystruct; class SomeClass { vector MySTL; }; int main() { return 0; } What am I doing wrong? Thanx, Marty [edited by - Marty666 on March 17, 2003 1:54:28 PM]
_____ /____ /|| | || MtY | ||_____|/Marty
Advertisement
Somehow this forum doesn't allow me to use > and < in this last message...

vector MySTL
should be:
vector < mystruct > MySTL
without ^these...^ spaces

Marty

[edited by - Marty666 on March 17, 2003 1:57:21 PM]
_____ /____ /|| | || MtY | ||_____|/Marty
place:

using namespace std;

at the end of all your include lines, or everytime you use something from the stl write std:: before the name of the variable, i.e. std::vector.

That should do it... hopefully...

#include <vector>

struct mystruct
{
int Value1, Value2;
}mystruct;

class SomeClass
{
vector MySTL;
};

int main()
{
return 0;
}

try this instead


#include <vector>

struct tagMystruct
{
int Value1, Value2;
}mystruct;

class SomeClass
{
public :

vector MySTL;

SomeClass();

};

SomeClass myClass;

int main()
{
// do what you like with some class

return 0;
}



Yes, that seems to be your primary problem (do using namespace std, or std::vector). Using your code, it has one other problem: it get''s the variable and the structure confused. (at least for VC.NET)

Try:

typedef struct
{
...
} mystruct;

instead of:

struct mystruct
{
...
} mystruct;

(then you also dont have a variable of type mystruct NAMED mystruct).
The STL classes are in the std (standard) namespace. So to use a vector, you would need to use the qualified name like:

std::vector

or you could put a using directive somewhere, like:

using namespace std;

or a using declaration like:

using std::vector;

in the file, to bring std::vector into the local namespace (which in this case would just be the global namespace). You could also use a typedef like:

typedef std::vector vector;

sorry , when i''ve posted someting went wrong with the tags on this forum i hope this will be the correct post
include

struct tagMystruct
{
int Value1, Value2;
}mystruct;

class SomeClass
{
public :

vector < tagMystruct > MySTL;

SomeClass();

};

SomeClass::SomeClass ()
{
// init code here
}

SomeClass myClass;

int main()
{
// do what you like with some class

return 0;
}



Thanx v71, but it allready was private in my real program and it gave errors before I even created anything of that class, but thanx anyways!
djones, I put std:: in front of the vectors, it works now, but what exactly did this do? c++ help is very vague about it.

I have an example that uses vectors just the same way as I did earlier, and that seems to work fine, somehow

Thanx,
Marty


[edited by - Marty666 on March 17, 2003 2:18:44 PM]
_____ /____ /|| | || MtY | ||_____|/Marty
to post source code in this forum you use

[ source ] and [ / source ] tags ... without those extra spaces ... and always in lower case
what putting std:: does is QUALIFY the name ... meaning gives it directions on where to look it up ...

you know how C++ allows you to define identical functions and type in different classes ... like this


  class A  {  enum ColorChannel    {    RED=1,    GREEN=2,    BLUE=3    };  int Foo(void);  };class B  {  enum ColorChannel    {    ALPHA=1    RED=2,    GREEN=3,    BLUE=4    };  int Foo(void);  };  


and the compiler figures out which function to call based on the type of the object ... well it can also be TOLD which one to call (when appropriate) ... such as

A::RED (would be 1)
B::RED (would be 2)
and NON-QUALIFIED
RED (would be an ERROR, cause it doesn''t know which one you want)

so are you starting to see, how there is an extra level of scope for names inside of classes ...

and then their are namespaces ... which are basically extra levels of scope, with no other reason for existing ... aka


  namespace boost{enum ColorChannel  {  RED=5  };int Foo(void);}  


well, just like the boost library ... the STANDARD C++ library is also in a namespace ... called "std" ... the REASON for this is so that they can add functions and features to the standard libraries, and they will not conflict with YOUR (and other 3rd parries) functions, types, and libraries.

So whenever you want to use part of the standard library .. you must either qualify it ... aka std::WHAT_YOU_WANT ... or tell the compiler to automatically bring the anything in the std namespace into the "global" namespace with a using declaration .. aka

using namespace std;

or even

using std::string;
using std::vector;

to bring just the string and vector class into the global namespace ...

NOTE - be careful putting a "using" declaration in a header file ... it can ruin everything ... especially if you ever try to wrap that in a namespace ... just pure chaos.

ALSO - when you see something like this ::Foo() (a function or type preceded by a :: with NO name in front of it) what the person is doing is QUALIFYING the name as being the global version of the function ... so that, you can still refer to the global version of the function, even if another version is in scope ...

This topic is closed to new replies.

Advertisement