Vector?? What's wrong?

What is wrong with the following codings while compiling in VC6?

#include <iostream>
#include <vector>

int main(){
      vector<int> v1(3);
      cout << v1;
      return 1;
}

Here is the error messages:

'vector' : undeclared identifier
type 'int' unexpected
'cout' : undeclared identifier
'v1' : undeclared identifier
'<<' : operator has no effect; expected operator with side-effect
huutriAsked:
Who is Participating?
 
KangaRooCommented:
>>   using std;
should be
   using namespace std;
0
 
KangaRooCommented:
1. try it with
   std::vector<int> v1(3);
or a
   using std;
before main()

2. cout << v1;
You can't stream vectors like that.
0
 
LucHoltkampCommented:
iostream is also part of namespace std.
So you should add
using namespace std;
before main
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
KangaRooCommented:
True, adding the (famous) using namespace std; is the easiest solution
0
 
jasonclarkeCommented:
You can stream out the vector by defining the operator, something like:

template <typename T>
ostream& operator<<(ostream& o, const vector<T>& vec)
{
    vector<T>::const_iterator i = vec.begin();
    o << "[";
    while (i != vec.end())
    {
        o << *i;
        ++i;
        if (i != vec.end()) o << ",";
    }
    o << "]";
    return o;
}
0
 
huutriAuthor Commented:
The problem with the question mainly concerns vector class in MS Visual C++ (vector identifier is not declared), not the stream out operator.
I am not sure why that problem occurs. I have checked include path but no use.
0
 
KangaRooCommented:
Have you actually tried to add
   using namespace std;
0
 
LucHoltkampCommented:
Yes huutri, the stream is also undefined, the compiler sais:
'cout' : undeclared identifier

Al you're problems will go away (except that the vector<>::operator<< is not defined, but jason gave you a solution for that) if you add:
using namespace std;
Do not reject answers before trying the solution...
Luc
0
 
huutriAuthor Commented:
I'd really appreciate your replies. That would be my mistakes to reject answer, so sorry about that.
0
 
KangaRooCommented:
No problem, especially since you've graded a comment ;)
Did you get it to work yet?
0
 
huutriAuthor Commented:
sure, it worked. Kan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.