-
operator overloading
//hello.. i need some help...
//i want to overload the << and the >> operators..
//in my main i have something like this:
IndexedList<string> list1;
string s;
cout << "Please enter the first list:" << endl;
cin >> s;
for(int i = s.length() ; i >0; i--) cin.putback(s.at(i-1));
cin >> list1;
cin >> s;
cout << list1;
//in my header i have something like this:
template<class ItemType>
std::istream& operator>> (std::istream& is, ItemType& item)
{
is >> item.data;
return is;
}
template< class ItemType>
std::ostream& operator<< (std::ostream& os, ItemType item)
{
os << item.data;
return os;
}
//when i run it i get these errors:
e:\project7\proj7\indexedlist.h(59) : error C2228: left of '.data' must have class/struct/union type
E:\project7\proj7\proj7.cpp(62) : see reference to function template instantiation 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl operator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,char *)' b
eing compiled
e:\project7\proj7\indexedlist.h(52) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
E:\project7\proj7\proj7.cpp(63) : see reference to function template instantiation 'class std::basic_istream<char,struct std::char_traits<char> > &__cdecl operator >>(class std::basic_istream<char,struct std::char_traits<char> > &,class std:
:basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled
e:\project7\proj7\indexedlist.h(52) : error C2039: 'data' : is not a member of 'IndexedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >'
E:\project7\proj7\proj7.cpp(66) : see reference to function template instantiation 'class std::basic_istream<char,struct std::char_traits<char> > &__cdecl operator >>(class std::basic_istream<char,struct std::char_traits<char> > &,class Inde
xedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)' being compiled
e:\project7\proj7\indexedlist.h(59) : error C2039: 'data' : is not a member of 'IndexedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >'
E:\project7\proj7\proj7.cpp(80) : see reference to function template instantiation 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl operator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Inde
xedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >)' being compiled
Error executing cl.exe.
//whats the problem here?
//thanks
//Akira
-
I'd say that you're trying to call your >>-operator with right hand data type that has no .data variable. Or then it isn't accessible (private, protected).