$ 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:
Anonymous2009-11-21 0:05
What the fuck, I have no idea what's going on either.
Name:
Anonymous2009-11-21 0:07
>>3
Yes, that's all correct actually. It's the line before that that it's crapping out on; it doesn't understand 'std::list<T*>::const_iterator' for some reason.
Strangely if you put in 'std::list<int>::const_iterator' instead, it parses (but obviously won't compile if you try to print a list of anything besides ints).
Basically, the compiler can't know ahead of time whether std::list<T>::const_iterator is a type or a member variable, so it assumes member. You have to tell it it's a type.
Sepples sucks.
Name:
Anonymous2009-11-21 0:24
Also, the way I found this is that I remembered reading in passing that some bizarre situations required typename where it ordinarily wouldn't (usually happens with typedef, i.e. all the const_iterator, const_pointer, etc. are all defined with typename). So I tried it and it worked, and having no idea why, I googled "c++ why do i need typename".
The C++ grammar is *insane*, by the way. The amount of context the compiler requires to figure this shit out is unbelievable. No wonder my builds are fucking slow.
Name:
Anonymous2009-11-21 0:27
>>5
You need a typename keyword before std::list<T*>::const_iterator. Otherwise, it assumes std::list<T*>::const_iterator is a value, not a type.
G++ is pretty shitty for not even mentioning this.
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*>.
>>11
You can print pointers actually. You get the address in hex.
It seems he wants to print a list of pointers, so changing it to <T> won't actually fix it; if he calls it with a list of pointers, deferencing the iterator in this case will still yield a pointer.
What he needs is the above code with <T>, and a template partial specialization with the same code for <T*> which dereferences twice.
Whenever I write a new class I just copy and paste in these two functions so I can easily debug with std::cout<<test;.
I'm terrible with keep track of whether it's an object or a pointer so I overloaded the pointer printing as well.
>>17
Yeah, then you need the two functions in >>14; they'll let you print a list of anything, pointers or not. Just remember there's no delimiters or anything. I'd print "list(" before and ")" after and "," between if I were you.