?Code:#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
ostream& M::print(ostream& stream) {
s::iterator i;
for(i = t->begin(); i != t->end(); ++i)
{
stream << *i;
}
}
Printable View
?Code:#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
ostream& M::print(ostream& stream) {
s::iterator i;
for(i = t->begin(); i != t->end(); ++i)
{
stream << *i;
}
}
Where is pointer t defined and what is the error message?
t is defined in the header file
and i get a huge list of errorsCode:.
.
.
private:
typedef list<ML> s;
s * t;
Code:M.cpp: In member function `std::ostream&
M::print(std::ostream&)':
M.cpp:56: error: no match for 'operator<<' in 'stream <<
(&i)->std::_List_iterator<_Tp, _Ref, _Ptr>::operator*() const [with _Tp =
ML, _Ref = ML&, _Ptr = ML*]()'
/usr/include/c++/3.3.2/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/3.3.2/bits/ostream.tcc:85: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
= std::char_traits<char>]
/usr/include/c++/3.3.2/bits/ostream.tcc:107: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/3.3.2/bits/ostream.tcc:179: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3.2/bits/ostream.tcc:216: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
.
.
.
list goes on..
Iīm not sure but shouldn't M::print() also be templated??
not sure..
but i've realised that when i have
that gives me a bunch of errorsCode:
ostream& M::print(ostream& stream) {
s::iterator i;
for(i = t->begin(); i != t->end(); ++i)
{
stream << *i;
}
}
but if i have
that gives me no problemsCode:ostream& M::print(ostream& stream) {
s::iterator i;
for(i = t->begin(); i != t->end(); ++i)
{
stream << "hello";
}
}
so im thinking that it could be the *i...
i've just been searching aroudn and i foudn this
not sure what that means tho..Code:if (! i->passed()) // iterators also provide operator ->
{
cout << "The student " << *i << " failed." << endl;
// provided that class Student provides the overloaded
// stream insertion operator <<
}
just out of curosity what happen when you instantiate your class (M) template with a primitive datatype?
what do you mean? i dont understand..
im pretty new to c++
Hmm, pretty new to c++...I donīt want to be rude but templates (particular class) shoudlnīt be learned to a "im pretty new to c++" in c++. You canīt run if you canīt walk :(. But I will make a try.
Primitive datatypes: Are datatypes that are built-in in c++ e.i int, float, double, char...
Template instantiation: Is when a function or a class is substituted with a buildt-in- or an user defined type.
hmmm yeah you've lost me.. haha
but now i'm defintely sure that the probelm is
when i do the printout of *i
i is the iterator for your list, which is of type ML. The ostream class doesn't know how to handle the '<<' operator with that class unless you define it. You'll need to overload the '<<' operator to work with an ostream and your ML class, or go about it by calling a member function of the ML class that will just print it's data types or something.
The FAQ and tutorials section will tell you how to go about overloading the '<<' operator (or the insertion operator in this case, if you're curious about a formal name...not to be confused with the bitshif left operator).