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: Anonymous 2009-11-21 0:00

No.  C++ is a pain in the ass.  Why are you trying to learn it?

Name: Anonymous 2009-11-21 0:03

I don't even know much SEPPLES, but the following is pretty obviously strange, if not wrong:
i=list.begin(); i!=list.end(); [b]i++[/b]
and does dereferencing i really does what you think it does?

Name: Anonymous 2009-11-21 0:05

What the fuck, I have no idea what's going on either.

Name: Anonymous 2009-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).

Name: Anonymous 2009-11-21 0:21

Ah, >>4,5-chan here, solution:

#include <iostream>
#include <list>

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


Here's an explanation: http://pages.cs.wisc.edu/~driscoll/typename.html

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: Anonymous 2009-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: Anonymous 2009-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.

http://pages.cs.wisc.edu/~driscoll/typename.html

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*>.

Name: Anonymous 2009-11-21 0:31

>>11
Need to reload /prog/ before posting.

Name: Anonymous 2009-11-21 0:33

>>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.

Name: Anonymous 2009-11-21 0:36

>>13
#include <iostream>
#include <list>

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

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

Name: Anonymous 2009-11-21 0:39

God, this is just horrible. It really, really makes me want to just write a linked list with macros or void*.

Name: Anonymous 2009-11-21 0:40

Thanks guys.

Didn't know that typename had a secondary usage like that. Well that's just good'ol sepples for ya: more exceptions than rules.

Name: Anonymous 2009-11-21 0:49

>>13
Actually I wanted to print the pointer:


std::ostream& operator<<(std::ostream& os, const Node& node){
    return node.toString(os);
}

std::ostream& operator<<(std::ostream& os, const Node* node){
    return node? os << (*node) : os;
}


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.

Name: Anonymous 2009-11-21 0:51

>>14
VALID PERL CODE

Name: Anonymous 2009-11-21 0:57

>>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.

Name: Anonymous 2009-11-21 2:43

>>20
Perl is more readable than pretty much anything, so it's not surprising.

Name: NULLNULLNULLNULL 2009-11-21 3:44

cout << (list<void*>)win32;
NULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULL

Name: Anonymous 2009-11-21 3:58

Sepples on Loonix? What is wrong with you?

Name: Anonymous 2009-11-21 7:14

>>6
IMPOSTER SPOTTED

Name: Anonymous 2009-11-21 9:28

>>24
Imposter spotter spotted

Name: Anonymous 2009-11-21 9:48

>>25
PUN MAKER SPOTTED

Name: Anonymous 2009-11-21 10:39

<pun type="amusing-modification-of-reference-material" rel="post" href="http://dis.4chan.org/read/prog/1258778338/26">PUN MARKER SPOTTED</pun>

Name: Anonymous 2009-11-21 11:02


(mapcar (function print) list)


The most descriptive you'll ever get, and laconic at the same time.

Name: Anonymous 2009-11-21 11:07

>>28

keep your fictional language to yourself, pal.

Name: Anonymous 2009-11-21 12:52

>>28
(map display list)
ha, it's shorter

Name: Anonymous 2009-11-21 13:08

mapM_ writeLn xs
better

Name: Anonymous 2009-11-21 13:49

>>32
Weird, too, since putStrLn completely fails at the task, but print would work alright.

Name: Anonymous 2009-11-21 17:04

>>28
(mapc #'princ list)

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