Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Abril7 en 22 Noviembre 2016, 21:03 pm



Título: Duda [Sobrecarga de operadores]
Publicado por: Abril7 en 22 Noviembre 2016, 21:03 pm
BUENOS DÍAS,DEBO ACLARAR QUE YA HABIA COMPILADO MI PROGRAMA SIN ERRORES EN UN MISMO ARCHIVO, PERO A LA HORA DE SEPARLO POR CLASES ME DA UN ERROR.

LA CLASE QUE ME DA ERROR ESTA COMPUESTA ASÍ:

#ifndef PREFERENCIA_H
#define PREFERENCIA_H
#include <iostream>
#include <string.h>
#include <fstream>
#include <stdlib.h>
#include "Cancion.h"
#include <ctype.h>

using namespace std;

class Preferencia
{
    public:
        float calidad, gusto;

        Preferencia(const float a,const float b)
        {
            calidad= a;
            gusto= b;
        }
};
Preferencia& operator +(const Preferencia &p1,const Preferencia &p2)
{
  return *(new Preferencia(p1.calidad + p2.calidad, p1.gusto + p2.gusto) );
}

#endif // PREFERENCIA_H

LA MANERA EN LA QUE LA USO EN EL MAIN:

    Preferencia A(50, 75 );
    Preferencia B(150, 175 );
    Preferencia C = A + B;

    cout << "A = " << A.calidad << ',' << A.gusto << "\n";
    cout << "B = " << B.calidad << ',' << B.gusto << "\n";
    cout << "C = " << C.calidad << ',' << C.gusto << "\n";
    cout<<endl;


NO TENIA NINGUN ERORR ANTES DE PONER ESA CLASE POR SEPARADA.

Esto es lo que me dice:

warning:suggest parentheses around assigment used as truth value (-Wparentheses)
In function 'ZN11PreferenciaCIEff':
multiple definition of 'operator+(Preferencia const&, Preferencia const&)' [line 24]
first defined here[line 24]
error:Id returned 1 exist status




Título: Re: Duda [Sobrecarga de operadores]
Publicado por: ivancea96 en 22 Noviembre 2016, 22:40 pm
El #include "preferencia.h" lo haces varias veces, en varios .cpp quizás?