I have written a code which has to iterate through a vector and print its contents. I am getting an error:
dfs.cpp:45:16: error: no match for ‘operator*’ (operand type is ‘const traits’) std::cout << *c << ' '; dfs.cpp:44:15: error: cannot bind ‘std::ostream {aka std::basic_ostream}’ lvalue to ‘std::basic_ostream&&’ std::cout<<*v<
However, the iteration works for a vector of type char
#include<iostream>
#include<list>
#include<vector>
#include<stdio.h>
using namespace std;
struct traits
{
int x;
bool visit;
std::list<int> friends;
};
int main()
{
cout << "Enter the number of employees: " << endl;
int noOfEmployees, noOfFriends;
cin>>noOfEmployees;
std::vector<traits> employees;
int i = 0; int j;
while(noOfEmployees--){
traits v;
v.x = i;
cout << "Enter the no of friends: " << endl;
cin >> noOfFriends;
while(noOfFriends--){
cin>>j;
v.friends.push_back(j);
}
v.visit = false;
employees.push_back(v);
}
std::vector<char> path;
path.push_back('a');
path.push_back('l');
path.push_back('i');
path.push_back('a');
for (std::vector<char>::const_iterator i = path.begin(); i != path.end(); ++i){
std::cout << *i << ' ';
}
for(std::vector<traits>::iterator v = employees.begin(); v != employees.end(); ++v){
std::cout<<*v<<endl;
}
}
I have seen few answers but I want to do this without operator overloading, what would be the correct or more C++-nic way?
operator<<
fortraits
, or print out the content oftraits
by yourself. - songyuanyaooperator<<
for a type you just created?char
is part of the language, of course printing it would be supported out of the box. - StoryTeller - Unslander Monica