Need lots of help with STL =[

Started by
11 comments, last by Brobanx 22 years, 4 months ago
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
Advertisement
///do this
#include <vector.h>

std::vector<int> someints;
The correct answer is:

#include

using namespace std;

vector intarray;

The "using namespace std;" is the key.
Visit my web site."I came, I saw, I coded."
Thanks a ton!
std::vector did the trick =].
using namespace std; is just a lot easier though, or you''ll encounter this problem with other standard types.
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 ]
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
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.hide.cppide_cpp.hide_cpp.cppide_cpp_preprocessor.hide_cpp_preprocessor.cppide_cpp_parser.hide_cpp_parser.cppide_cpp_compiler.hide_cpp_compiler.cppide_java.hide_java.cppide_java_compiler.hide_java_compiler.cppide_java_compiler_dynamic.hide_java_compiler_dynamic.cppide_java_compiler_static.hide_java_compiler_static.cppide_java_interpreter.hide_java_interpreter.cppNamespaces: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
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
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com

This topic is closed to new replies.

Advertisement