Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Sepples is super hard

Name: Anonymous 2009-11-20 23:38


$ cat test.cc

#include <iostream>
#include <list>

template <class T>
std::ostream& operator<<(std::ostream& os, const std::list<T*>& list){
    std::list<T*>::const_iterator i;
    for( i=list.begin(); i!=list.end(); i++) {
        os << *i;
    }
    return os;
}

$ g++ test.cc
test.cc: In function ‘std::ostream& operator<<(std::ostream&, const std::list<T*, std::allocator<T*> >&)’:
test.cc:6: error: expected ‘;’ before ‘i’
test.cc:7: error: ‘i’ was not declared in this scope

                                                         
I'm too retarded to figure out how to print a list of objects in sepples. Should I just become an hero now, /prog/?

Name: HMA 2009-11-21 0:29

You're taking a list<T*>, then you do os << *i;. If the list is as you seem to want a list of pointer, *i will dereference the iterator and give you the pointer, not the object, and I don't think you can print a pointer directly. You'd have to do *(*i) to first dereference the iterator, then the pointer.

That being said, GCC is apparently being a retard. What version do you use? This compiles fine on VC++. Also, give it a try using a list<T> instead of list<T*>.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List