Operator<< Compile Error

For the following piece of code:
1
2
for (int i = 0; i < trn.getSize(); i++)
	cout << *(trn[i]) << endl;


where operator << is defined as
1
2
3
4
5
6
7
std::ostream& operator <<(std::ostream& o, const Planet& p)
{
    o << p.getSun().getName() << " " << p.getName() << "," << p.numNeighs() << "," << p.getMSINI() << "," << p.getAxis() << ",";
    o << p.getPer() << "," << p.getEcc() << "," << p.getOm() << "," << p.getT0() << "," << p.getK();

    return o;
}



I get the following error:

error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
cout << *(trn[i]) << endl;
^


What is the most likely cause for this?
please provide enough code to reproduce your issue.
As a blind guess, you've got another overload, which instead tries to move the stream.


Also, ¿why is `trn' storing pointers?
Also, ¿why is `trn' storing pointers?


It's a way to pass items into the array by reference since making it take references won't work (pointers to references issue)

As for the other overload, that's unlikely since this is the only operator<< overload for the Planet class.

Info Update: I also seem to be getting the same compiler error when passing a const Planet& (from an array of Planets) into the ostream.
Last edited on
I also seem to be getting a compiler error when passing a const Planet& (from an array of Planets) into the ostream.

Thankyou for not telling us what the error message is, and for not showing us all the code we'd need to see to be able to solve the problem. This is so much more fun when we have to play guessing games.

[/sarcasm]
Sorry, but it's the same compile error I got with the previous code.
Are there templates involved?
Yes, now that I think about it. All of these examples use the templated class ]ArrayList<T>. trn is an ArrayList<Planet*>, while planets is an ArrayList<Planet>. Both of the lines of code causing errors are returning a Planet&, which I've done by overloading operator [] in my ArrayList class. The only confusing thing is that the Planet class doesn't have any templated objects in it.
Topic archived. No new replies allowed.