Archived

This topic is now archived and is closed to further replies.

I cannot use STL, why ???

This topic is 5503 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 tried to make a vector from STL, but the compiler gives me this message all the time
  
error C2143: syntax error : missing ';' before '<'
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2501: 'vector' : missing storage-class or type specifiers
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2059: syntax error : '<'
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2238: unexpected token(s) preceding ';'
D3DApp.cpp
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2143: syntax error : missing ';' before '<'
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2501: 'vector' : missing storage-class or type specifiers
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2059: syntax error : '<'
d:\game project\project code & program\engine\copy of hotgame (plp)\3dscene.h(73) : error C2238: unexpected token(s) preceding ';'
   
I know that I didn't make any syntax error, but why this is happening ?? This is my declaration code : vector visible_set; simple, huh ?? [edited by - Stoffel on November 22, 2002 7:14:14 PM]

Share this post


Link to post
Share on other sites
All the standard library classes and functions are wrapped within the namespace ''std''.

To make use of anything in a namespace you have to either:

1) Already be in the namespace - if you have your own namespace, namespace yanuart { } for example, and are within the namespace scope then you won''t have problems accessing yanuart specific classes, functions etc.

2) Outside of the namspace you need to let the compiler know exactly what function, class, variable etc you want to use from the namespace. There are a few ways to do this:-

One is to explcitly qualify as the_Programmer_Goblin says. ie std::vector .
Another is to say ''using std::vector;'' somewhere before you use the vector. This shouldn''t be done in a header file, only in a cpp. You can just refer to ''std::vector'' as ''vector'' (unless there is a vector from a different namespace visible as well, then you still need to explicitly qualify as above).
Finally you can just drag everything in by saying ''using namespace std;''. Again don''t put this in a header.

Its all about stopping ambiguity for the compiler.

Share this post


Link to post
Share on other sites
if you want to make a < or > you have to type '&' followed by 'lt;' or '&' follow by 'gt;' (html for less-than and greater-than)


[edited by - petewood on November 22, 2002 6:33:19 AM]

Share this post


Link to post
Share on other sites
It''s OK, I got it all fixed for good now, it seems that I haven''t include the header files.. which is weird.. I thought the compiler automatically include stdio/lib ??

anyway this bring another question.. for a standard Win32 programming which header files shoud I inculde in my code ??

Share this post


Link to post
Share on other sites
I had a problem with replacing the old fstream.h to the new fstream which is also a class of STL.
when 'using namespace' it's important that there will be no old liberaries, for example:

    
#include fstream.h
#include <vector>
using namespace

is bad because of a mixure of old and new libararies

instead you should write

        
#include fstream.h
#include <std::vector>




[edited by - alissre on November 22, 2002 11:58:30 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by alissre
I had a problem with replacing the old fstream.h to the new fstream which is also a class of STL.
when ''using namespace'' it''s important that there will be no old liberaries, for example:

#include fstream.h
#include <vector>
using namespace

is bad because of a mixure of old and new libararies

instead you should write

#include fstream.h
#include <std::vector&ht;



Er, something went wrong there. fstream.h is the old deprecated header. Don''t use this unless you have to for backwards compatibility. std::vector is not a file so you can''t include it.

How about something more like this:
#include <vector>
#include <fstream>

int main
{
using namespace std;
}

500 try1

- Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

Share this post


Link to post
Share on other sites
just to clear things up

quote:
Original post by flangazor
#include <vector>
std::vector <someType> myVector;
This is the correct answer.

Yes, this is the correct answer. Ignore other different posts.


My compiler generates one error message: "does not compile."

[edited by - alnite on November 23, 2002 4:36:34 AM]

Share this post


Link to post
Share on other sites