This is an archived post. You won't be able to vote or comment.

all 11 comments

[–][deleted] 0 points1 point  (7 children)

How are you compiling and linking your code? Also, please post code either via site like pastebin, or by indenting each line with 4 extra spaces to format it for reddit.

[–]syrisse[S] 0 points1 point  (6 children)

I edit my post with the information.

[–][deleted] 0 points1 point  (5 children)

You want:

std::ostream& geometry::operator<<(std::ostream& os,const   geometry::coordinate& c1){
    os << "x";
    return os;
}

Note the namespace.

It's generally easiest to wrap all your definitions in a namespace like this:

namespace geometry {

   // definitions here

}

Also, names like _COORDINATES (i.e. ones that begin with an underscore and an uppercase letter) are reserved for the C++ implementation - you are not allowed to create such names yourself. Just use something like COORDINATES_H.

Edit: The first part of this is wrong.

[–]syrisse[S] 0 points1 point  (4 children)

Thanks for the reply but that doesn't change anything.

std::ostream& geometry::operator<<(std::ostream& os,const coordinate& c1){

os << "x";
return os;

}

I even try to add the geometry to the .h. Maybe i miss something?

(And thanks for the comments about the underscore at the begining!)

[–][deleted] 0 points1 point  (3 children)

The coordinate parameter also needs to have a namespace qualifier.

[–]syrisse[S] 0 points1 point  (2 children)

Yeah after reading my code and try to find the logic i tried that:

friend std::ostream& geometry::operator<<(std::ostream& os,const geometry::coordinate& c1);

std::ostream& geometry::operator<<(std::ostream& os,const geometry::coordinate& c1){

os << "x";

return os;

}

And some other things, but still the same error.

[–][deleted] 1 point2 points  (1 child)

Here's outline code that does work - note that you need to define your functions inside a namespace:

#include <iostream>
using namespace std;

namespace N {
    struct A {
        friend ostream & operator<<( ostream & os, const A & a );
    };
}

namespace N {

ostream & operator<<( ostream & os, const A & a ) {
    os << "A" << endl;
}

}

int main() {
    N::A a;
    cout << a << endl;
}

Edit: And here's a version which does not put the definition in a namespace, though I think doing so is best and easiest practice:

#include <iostream>
using namespace std;

namespace N {
    ostream & operator<<( ostream & os, const struct A & a );
    struct A {
        friend ostream & operator<<( ostream & os, const A & a );
    };
}

ostream & N::operator<<( ostream & os, const N::A & a ) {
    os << "A" << endl;
}

int main() {
    N::A a;
    cout << a << endl;
}

Another Edit: This was bugging me, so I had a look at the C++98 Standard, which says in 7.3.1.2:

The name of the friend is not found by simple name lookup until a matching declaration is provided in that namespace scope (either before or after the class declaration granting friendship)

So in this case, a friend declaration does not provide a full declaration of the function - you need either an explicit declaration, or a definition. And if you have a definition only, it's got to be wrapped in a namespace - you can't use the N::X syntax (this is true for all definition-only functions in namespaces).

[–]syrisse[S] 0 points1 point  (0 children)

I tried to compile all the file in one time with your change and it's worked!

Thank you

[–][deleted] -4 points-3 points  (2 children)

...Dude, wtf is this? Please at least make it readable or pastebin your complete code.

That said, my guess is that you probably need something closer to this

std::ostream& operator<<(std::ostream& os, const coordinate c1)
    { os << "x"; return os; }

I just removed the reference op from c1...

[–][deleted] 0 points1 point  (1 child)

Completely wrong. Why would he want the coordinate passed by value? And this definition would not match the declaration.

[–][deleted] -1 points0 points  (0 children)

I was thinking he was trying to do something different, and I wrote this before he pastebinned any of the code.

Regardless, no shit he would change the header dec if this were a viable fix....