0

Trato de hacer una simple suma de dos objetos y me salta error llevo tiempo tratando de solucionarlo.

Complejo.cpp

#include "Complejo.h"

Complejo::Complejo(double real, double complejo)
{
    this->real = real;
    this->complejo = complejo;
};


Complejo operator+(Complejo a, Complejo b)
{
    Complejo temporal(0.0, 0.0);
    temporal.real = a.real + b.real;
    temporal.complejo = a.complejo + b.complejo;;
    return temporal;
};

Complejo.h

#ifndef COMPLEJO_H_
#define COMPLEJO_H_

class Complejo
{
public:
    Complejo(double, double);
    double real;
    double complejo;
};

#endif

test_complejo.cpp

#include <iostream>
#include "Complejo.h"
using namespace std;

int main()
{
    Complejo unNumeroComplejo(43.5, 56.4);
    Complejo otroNumeroComplejo(34.5, 45.6);
    Complejo suma = unNumeroComplejo + otroNumeroComplejo;

    cout << "hola mundo" << endl;
    return 0;
};

Error

03:14:49 **** Incremental Build of configuration Debug for project teoriaCatedraCalvo ****
make all 
Building file: ../sobrecargaDeOperadores/Complejo.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"sobrecargaDeOperadores/Complejo.d" -MT"sobrecargaDeOperadores/Complejo.o" -o "sobrecargaDeOperadores/Complejo.o" "../sobrecargaDeOperadores/Complejo.cpp"
Finished building: ../sobrecargaDeOperadores/Complejo.cpp

Building file: ../sobrecargaDeOperadores/test_complejo.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"sobrecargaDeOperadores/test_complejo.d" -MT"sobrecargaDeOperadores/test_complejo.o" -o "sobrecargaDeOperadores/test_complejo.o" "../sobrecargaDeOperadores/test_complejo.cpp"
../sobrecargaDeOperadores/test_complejo.cpp: In function ‘int main()’:
../sobrecargaDeOperadores/test_complejo.cpp:9:35: error: no match for ‘operator+’ (operand types are ‘Complejo’ and ‘Complejo’)
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                  ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/7/bits/char_traits.h:39,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/stl_iterator.h:397:5: note: candidate: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
     ^~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:397:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::reverse_iterator<_Iterator>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/7/bits/char_traits.h:39,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/stl_iterator.h:1198:5: note: candidate: template<class _Iterator> std::move_iterator<_IteratorL> std::operator+(typename std::move_iterator<_IteratorL>::difference_type, const std::move_iterator<_IteratorL>&)
     operator+(typename move_iterator<_Iterator>::difference_type __n,
     ^~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:1198:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::move_iterator<_IteratorL>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5876:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5876:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:53:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.tcc:1157:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(const _CharT* __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.tcc:1157:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   mismatched types ‘const _CharT*’ and ‘Complejo’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:53:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.tcc:1173:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.tcc:1173:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5913:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5913:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5929:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5929:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5941:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5941:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5947:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5947:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5953:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5953:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5965:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)
     operator+(const _CharT* __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5965:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   mismatched types ‘const _CharT*’ and ‘Complejo’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5971:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)
     operator+(_CharT __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5971:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5977:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _CharT*)
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5977:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from ../sobrecargaDeOperadores/test_complejo.cpp:1:
/usr/include/c++/7/bits/basic_string.h:5983:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, _CharT)
     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
     ^~~~~~~~
/usr/include/c++/7/bits/basic_string.h:5983:5: note:   template argument deduction/substitution failed:
../sobrecargaDeOperadores/test_complejo.cpp:9:37: note:   ‘Complejo’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
                                     ^~~~~~~~~~~~~~~~~~
../sobrecargaDeOperadores/test_complejo.cpp:9:11: warning: unused variable ‘suma’ [-Wunused-variable]
  Complejo suma = unNumeroComplejo + otroNumeroComplejo;
           ^~~~
make: *** [sobrecargaDeOperadores/test_complejo.o] Error 1
sobrecargaDeOperadores/subdir.mk:21: recipe for target 'sobrecargaDeOperadores/test_complejo.o' failed

03:14:50 Build Finished (took 1s.218ms)
6

1 respuesta 1

1

Estas poniendo el operator+ sin definirlo en el archivo .h, por lo que no encuentra el operador. Además el propio operator+ solo necesita una variable, ya que en la propia clase lleva el otro objeto, y necesita que los parámetros que le pases sean const param&.

Te pongo un ejemplo con tu código que resuelve el problema:

Complejo.h

#ifndef COMPLEJO_H_
#define COMPLEJO_H_

class Complejo
{
public:
    Complejo(double, double);
    double real;
    double complejo;

    Complejo& operator+(const Complejo &a);
};

#endif

Complejo.cpp

#include "Complejo.h"

Complejo::Complejo(double real, double complejo)
{
    this->real = real;
    this->complejo = complejo;
};


Complejo& Complejo::operator+(const Complejo &a)
{
    Complejo temporal(0.0, 0.0);
    temporal.real = a.real + real;
    temporal.complejo = a.complejo + complejo;;
    return temporal;
};
7
  • No se si te has dado cuenta de que esta pregunta es un duplicado de otra que ya se respondió en su dia... mantener material duplicado no ayuda a mantener el portal convenientemente limpio
    – eferion
    el 1 mar. 2018 a las 11:15
  • No me había fijado ya los duplicados que he visto otras veces lo suele poner en le titulo o en la propia pregunta bien claro. Ahora veo que esta en los comentarios, intentaré estar mas atento para la siguiente.
    – Wyrncael
    el 1 mar. 2018 a las 11:20
  • Eso es cuando el motivo de cierre recibe la cantidad suficiente de votos. Mientras la pregunta está abierta el motivo aparece como comentario
    – eferion
    el 1 mar. 2018 a las 11:21
  • Entonces, ¿debería borrar esta respuesta?
    – Wyrncael
    el 1 mar. 2018 a las 11:23
  • Revisa la otra pregunta... si entiendes que la pregunta es la misma lo más recomendable sería borrar esta respuesta y, si procede, moverla a la otra pregunta
    – eferion
    el 1 mar. 2018 a las 11:27

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.