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

Pages: 1-

Why oh why, /prog/, why?

Name: Anonymous 2010-12-02 23:00

vector iteration, working in one instance, not working in the next. I don't understand.

% cat vector.cpp
#include <iostream>
#include <cstdio>
#include <vector>

template<typename T>
std::ostream &operator<<(std::ostream &o, std::vector<T> &v)
{
        for(typename std::vector<T>::iterator it = v.begin(); it != v.end(); ++it)
                std::cout << *it;
}

int main(void)
{
        std::vector<int> v;

        std::cout.sync_with_stdio();
        setbuf(stdout, NULL);

        for(int i = 0; i < 10; i++)
                v.push_back(i);

        for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
                std::cout << *it << std::endl;

        std::cout << v << std::endl;

        return 0;
}
% make vector
g++ -g -o vector vector.cpp
% ./vector
0
1
2
3
4
5
6
7
8
9
0123456789zsh: segmentation fault  ./vector
%

Name: Anonymous 2010-12-02 23:09

Stop using Sepples.

Name: Anonymous 2010-12-02 23:47

>>2
this, use Sea

Name: Anonymous 2010-12-02 23:48

Good luck using GDB.

Name: Anonymous 2010-12-03 2:56

g++ -Wall vector.cpp -o vector

Name: Anonymous 2010-12-03 2:56

You didn't return a reference to the ostream in the overloaded function. This thread has ended peacefully.

Name: Anonymous 2010-12-03 3:20

>>6
Yes, you can tell just by the output that the function performed the iteration correctly. The seg fault occurs in when trying to do:
v << std::endl;
when there is no reference to an ostream after returning from the function.

Name: Anonymous 2010-12-03 4:53

>std::

tryusing namespace std;

Name: Anonymous 2010-12-03 5:42

>>8
Namespace pollution considered harmful. Please go back to /comp/ or Reddit.

Name: Anonymous 2010-12-03 11:58

>>9
Not using using is considered harmful too.
Not using² my anus

Name: >>1 2010-12-04 8:39

>>5-7
FUCK
You'd think it'd warn by default, especially with references. I mean valgrind doesn't even mention uninitialised data. damn...

Anyway, back to /pr/ for me I guess.

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