Print out a vector

Hello. I have a function that excepts a vector of structs and I want to use an iterator and a cout statement to print the vector to the screen. Visual Studio is giving me a C2679 error and it saying "there is no acceptable conversion". Why is this happening and how does a newbie interpret that error?

1
2
3
4
5
6
7
 
void print(vector<employee> empIn)
{
	vector<employee>::iterator it;
	for( it = empIn.begin(); it != empIn.end(); ++it )
		cout << *it;
}


Thanks.
Have you overloaded operator << to print employees?

I mean something like this:

1
2
3
4
5
6
7
ostream & operator<<(ostream & os, const employee & e)
{
    os << e.name << endl;
    //...

    return os;
}

Also, it's a good idea to pass your vector by const reference:

1
2
3
4
5
void print(const vector<employee> & empIn)
{
    vector<employee>::const_iterator it;
    //...
}

Thanks for the reply.
Is overloading the operator << the only way to do it?
Thanks for the const reference tip.
Well, I guess you could also make an employee know how to
print itself and then use this inside your print function:

1
2
3
4
5
6
7
8
9
10
11
12
class employee
{
private:
    //...
public:
    //...
    void Print() const
    {
        cout << name << endl;
        //...
    }
};

1
2
3
4
5
6
7
void print(const vector<employee> & empIn)
{
    vector<employee>::const_iterator it;
    
    for(it=empIn.begin(); it!=empIn.end(); ++it)
        it->Print();
}
Thanks m4ster r0shi for the help. I got it!
Topic archived. No new replies allowed.