c ++ wiele definicji operatora <<

Próbuję zastąpić<< operator dla klasy. Celem jest zasadniczo wdrożenietoString() jak zachowanie dla mojej klasy, więc wysyłanie go docout wygeneruje użyteczne wyjście. Używając fałszywego przykładu, mam poniższy kod. Gdy próbuję się skompilować, pojawia się błąd, który powoduje błąd:

$ g++ main.cpp Rectangle.cpp
/tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)':
Rectangle.cpp:(.text+0x0): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)'
/tmp/ccLU2LLE.o:main.cpp:(.text+0x0): first defined here

Nie mogę zrozumieć, dlaczego tak się dzieje. mój kod jest poniżej:

Rectangle.h:

#include <iostream>
using namespace std;

class CRectangle {
    private:
        int x, y;
        friend ostream& operator<<(ostream& out, const CRectangle& r);
    public:
        void set_values (int,int);
        int area ();
};

ostream& operator<<(ostream& out, const CRectangle& r){
    return out << "Rectangle: " << r.x << ", " << r.y;
}

Rectangle.cpp:

#include "Rectangle.h"

using namespace std;

int CRectangle::area (){
    return x*y;
}

void CRectangle::set_values (int a, int b) {
    x = a;
    y = b;
}

main.cpp:

#include <iostream>
#include "Rectangle.h"

using namespace std;

int main () {
    CRectangle rect;
    rect.set_values (3,4);
    cout << "area: " << rect.area();
    return 0;
}

questionAnswers(2)

yourAnswerToTheQuestion