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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
KangaRooCommented:
>>   using std;
should be
   using namespace std;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LucHoltkampCommented:
iostream is also part of namespace std.
So you should add
using namespace std;
before main
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

KangaRooCommented:
True, adding the (famous) using namespace std; is the easiest solution
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;
}
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.
KangaRooCommented:
Have you actually tried to add
   using namespace std;
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
huutriAuthor Commented:
I'd really appreciate your replies. That would be my mistakes to reject answer, so sorry about that.
KangaRooCommented:
No problem, especially since you've graded a comment ;)
Did you get it to work yet?
huutriAuthor Commented:
sure, it worked. Kan
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.