std::ostream_iterator with non std object

E

er

Hello,

Could someone please suggest a solution for this that works with
std::eek:stream_iterator:

struct foo
{

foo(){}

private:
friend std::eek:stream & operator<<(std::eek:stream &os, const foo&
p);
};

std::eek:stream& operator<<( std::eek:stream& os, foo const& t)
{
// whatever
}

typedef std::vector<foo> vec_;
vec_ vec; vec.push_back( foo() );

std::copy(
vec.begin(),
vec.end(),
std::eek:stream_iterator<foo>( std::cout, " ")
);

error : no match for call to std::eek:stream_iterator<foo>( foo& );
 
J

Jonathan Lee

Hello,

Could someone please suggest a solution for this that works with
std::eek:stream_iterator:

*shrugs*
Works fine for me with minimal changes:

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

struct foo {
foo(){}
private:
friend std::eek:stream & operator<<(std::eek:stream &os, const foo& p);
};

std::eek:stream& operator<<( std::eek:stream& os, foo const& t) {
os << "derp";
return os;
}

int main() {
std::vector<foo> vec;
vec.push_back( foo() );

std::copy(vec.begin(), vec.end(),
std::eek:stream_iterator<foo>( std::cout, " ")
);
return 0;
}
--Jonathan
 
E

er

*shrugs*
Works fine for me with minimal changes:

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

struct foo {
    foo(){}
private:
    friend std::eek:stream & operator<<(std::eek:stream &os, const foo& p);

};

std::eek:stream& operator<<( std::eek:stream& os, foo const& t) {
    os << "derp";
    return os;

}

int main() {
    std::vector<foo> vec;
    vec.push_back( foo() );

    std::copy(vec.begin(), vec.end(),
        std::eek:stream_iterator<foo>( std::cout, " ")
    );
    return 0;}

--Jonathan

Besides the include statements, could you please point out the
specific changes?

PS: I Use OS X 10.6.4, GCC 4.2.
 
J

Jonathan Lee

Besides the include statements, could you please point out the
specific changes?

PS: I Use OS X 10.6.4, GCC 4.2.

Um... I put actual code in the operator<< and I wrapped it in main. I
really didn't do very much. I think your code is basically fine.

Fedora Linux/gcc 4.4 (for comparison).

--Jonathan
 
E

er

really didn't do very much. I think your code is basically fine.

And yet

std::eek:stream_iterator<foo> output1(std::cout, " ");
output( foo() );

still gives

error: no match for call to '(std::eek:stream_iterator<foo, char,
std::char_traits<char> >) (foo&)'

with OS X 10.6.4, GCC 4.2.

No big deal, I'll create a custom print functor for foo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top