Overloading << with duplicate character character error

My head is about to explode. I cannot figure out what I am doing wrong when trying to overload the '<<operator with two classes (punto and vector). Here is the code, the code is written in the class header file from classes:

   std::ostream& operator << (ostream& salida, const punto& origen)
    {   
        // Se escriben los campos separados por el signo
        salida << "Punto --> x: " << origen.xf << " , y: " << origen.yf;
        return salida;
    }

    std::ostream& operator << (ostream& salida, const vector& origen)
    {
        // Se escriben los campos separados por el signo
        salida << "Punto --> x: " << origen.p1.xf << " , y: " << origen.p1.yf;
            return salida;
    }

      

There is an error in the linking step and there is no double reference with the class header because this is a simple example.

enter image description here

+3


source share


1 answer


This particular error means the function will compile into two different translation units. This is most likely to happen if you put the function definition in the header and include it in two different source files.

You have basically two solutions:



  • Declare, don't define, your function in the header. Define (implement) it in your source file.
  • Declare your function as static or inline.
+6


source







All Articles