Archived

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

Brobanx

Need lots of help with STL =[

Recommended Posts

I've been trying all day just trying to make a vector! I almost made my own CArray class before starting to look everywhere on tutorials for STL and such, but none of them tell me anything about what I need to know.
        
#include <vector>


vector<int> intarray;
      
Every time I try to compile, it gives me the error C2501: 'vector' : missing storage-class or type specifiers. I looked at my include files and all the STL files are in there (I'm using Visual C++ 6.0), but for some reason the compiler's thinking that vector isn't defined. Edited by - Brobanx on December 17, 2001 7:43:13 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
using namespace std; is just a lot easier though, or you''ll encounter this problem with other standard types.

Share this post


Link to post
Share on other sites
Yeah, but that''s not a problem: it''s a feature. The ''std::'' bit is there to distinguish the class name from any others that might exist already, so that using an include full of new classes won''t break existing code.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
using std::vector;
is the ''preferred'' method, or so I''ve been told.
That way you just get what you want, not everything in namespace...

Magmai Kai Holmlor

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

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
The way we do it here (at work):

In header files, "using" is not allowed. Period. Every STL name (e.g. in arguments or as members of classes) has std:: in front of it. Even "using std::list" is dangerous in a .h file--you''ll dictate that every list in every .h file included after that one for every client is a std::list.

In .c files, use whatever you want. Mostly we import the entire std namespace, unless we have conflicts in our files with other modules, then we''re selective.

Share this post


Link to post
Share on other sites
Stoffel:

Why not use a project namespace? Then divy it up into namespaces for organizational purposes. I would think that this approach would cut down on the possibilities of name conflicts within the project, too.

I give each source/header file pair a project namespace and one or more subnamespaces according to what it does, i.e.:

Filenames:

ide.h
ide.cpp
ide_cpp.h
ide_cpp.cpp
ide_cpp_preprocessor.h
ide_cpp_preprocessor.cpp
ide_cpp_parser.h
ide_cpp_parser.cpp
ide_cpp_compiler.h
ide_cpp_compiler.cpp
ide_java.h
ide_java.cpp
ide_java_compiler.h
ide_java_compiler.cpp
ide_java_compiler_dynamic.h
ide_java_compiler_dynamic.cpp
ide_java_compiler_static.h
ide_java_compiler_static.cpp
ide_java_interpreter.h
ide_java_interpreter.cpp

Namespaces:

ide
cpp
preprocessor
parser
compiler
java
compiler
dynamic
static
interpreter


(This approach would be great if MSVC's ClassView would recognize namespaces. I suppose you could replace the filename prefixes with folders and subfolders, and then use FileView to browse the project. I think that this is similar to what Java does automatically - creating namespaces from filenames.)

Then I place non-namespace using statements at the top of the smallest possible scope to indicate dependencies. This separates the dependencies from the actual code and makes the bodies of the classes, functions, and so forth much more readable without the possibility of messing up any other files.

The frustrating thing about classes is the way that namespaces interact with classes. Why can I not place a namespace within a class to add optional prefixes? Instead I must resort to using multi-word identifiers with underscores or capitalization...blech.

- null_pointer

Edited by - null_pointer on December 18, 2001 11:01:04 AM

Share this post


Link to post
Share on other sites
using namepace std; is ok if you plan on using the STL version of the object throughout. If you only want to use std::string and you have defined your own vector class, then don''t use the namespace line. The std::vector will be used instead of your class.

-----------------------------
kevin@mayday-anime.com
http://games.mayday-anime.com

Share this post


Link to post
Share on other sites
quote:
Original post by null_pointer
Why not use a project namespace? Then divy it up into namespaces for organizational purposes. I would think that this approach would cut down on the possibilities of name conflicts within the project, too.


We do that too. None of the project files are allowed to say "using prj" in their headers, but they may in the .cpp files (following the rule in my last post).

Share this post


Link to post
Share on other sites
What about the idea of matching filenames to namespaces, with each file pair having one namespace. Then it would not matter if the header file contains using statements, because no matter which file is included in which other files, no using expressions would conflict.

Here is an example where one part of a project is using the version of the standard library provided with the compiler, and another part is using the SGI version of the standard library:
// ide_cpp_preprocessor.h

#include <string>
#include <istream>
#include <ostream>

namespace ide {
namespace cpp {
namespace preprocessor {

using std::string;
using std::istream;
using std::ostream;

namespace symbol {
bool check (const string& name);
void replace(const string& name, const ostream& out);
}
}
}
}

// ide_cpp_parser.cpp

#include "ide_cpp_preprocessor.h"

#include <sgi/string>
#include <sgi/istream>
#include <sgi/ostream>

namespace ide {
namespace cpp {
namespace parser {

using sgi::string;
using sgi::istream;
using sgi::ostream;

namespace read {
name(istream& in, ostream& out) { /* ... */ } // no conflict
}
}
}
}


Edited by - null_pointer on December 18, 2001 2:27:52 PM

Share this post


Link to post
Share on other sites
That''s alot of bookkeeping...
I also force myself to make typedef for every STL class I create:


typedef std::vector tyVectorMyStuff;
tyVectorMyStuff m_vMyStuff;


Magmai Kai Holmlor

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

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites