Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie - vectors - tearing my hair out

Status
Not open for further replies.

williegofar

Programmer
Feb 3, 2003
11
GB
last week i had a look at the basics of vectors, without a problem. i deleted that code and this week wanted to use them in a small program. i don't know what i'm doing wrong, but the following simple little program won't compile;

#include "stdafx.h"
#include <vector>

vector<int> blob;

int main(int argc, char* argv[])
{
return 0;
}

the errors are;

D:\source\test54\test54.cpp(7) : error C2143: syntax error : missing ';' before '<'
D:\source\test54\test54.cpp(7) : error C2501: 'vector' : missing storage-class or type specifiers
D:\source\test54\test54.cpp(7) : error C2143: syntax error : missing ';' before '<'

Help!, this is driving me nuts
 
the header file is not being found. Make sure your path environment variable containst the path to vector. Also, you could try changing vector to vector.h.

Matt
 
Thanks for the reply

It does appear to be finding the header file. If I change #include <vector> to <vector.h> I get;

D:\source\test54\test54.cpp(5) : fatal error C1083: Cannot open include file: 'vector.h': No such file or directory

The file 'vector' in the include directory is dated june 98 so I assume it is as it should be.

Any ideas?
 
Yes, your problem is that vector is in the namespace std::

Try changing the line vector<int> blob; to

std::vector<int> blob;

or you can do something like this:

using std::vector;

vector<int> blob;
 
Fantastic, it worked!

As suggested I used std::vector<int> blob;

I’m unclear as to what std:: means and why I have to use it, but that can wait for another day.

Thank you very much for your help.
 
Modern C++ has a concept known as namespaces. In order to keep the names in the standard template library from conflicting with those that may already be in your program they are declared as belonging to the &quot;std&quot; namespace. To use such names you need to specify which namespace they are from, this can be done in several ways, as in the example. Read more about it in the third edition of Stroustrup's book, &quot;The C++ Programming Language&quot;.

Stuff that isn't declared as being in a particular namespace is in the &quot;global&quot; namespace. This is denoted explicitly as ::name, but it's the default if you don't indicate it, so the unqualified version of the name is assumed to be global.

You can bring entire namespaces into the global namespace with a declaration like:

using namespace std;

The problem is that this brings ALL the names in &quot;std&quot; into the global namespace which may lead to a &quot;polluted&quot; namespace and many conflicts. It is much better to only bring in the names you need with specific declarations like:

using std::vector;

or by referring to them explicitly each time you use them:

std::vector<int> blob;

Hope this helps.

DJ
 
Good Work

I have been tearing my hair out over this one aswell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top