first of all and based on your great explanations Here
I am now trying to define an overload function for the + operator but external to the class. First of all, I ask you not to be "scared" since the definition I have made by myself and may contain what for you are "serious" errors, but hey, I'm learning. The issue is that it fails me when I perform the sum in the main (with overload obviously):
res = p+q;
You see, this is what I've built to try to understand overloading and implement it:
MAIN:
#include <iostream>
#include "Racional.h"
using namespace std;
int main()
{
Racional res, p(1,1), q(2,2);
res = p+q;
}
RATIONAL CLASS:
#ifndef Racional_h_
#define Racional_h_
class Racional
{
private:
int numerador;
int denominador;
public:
Racional(int = 0, int = 0);
int obtenerNumerador() const;
int obtenerDenominador() const;
void almacenarSuma(int num, int den);
};
#endif
RATIONAL CLASS IMPLEMENTATION AND EXTERNAL FUNCTIONS:
#include <iostream>
#include "Racional.h"
using namespace std;
Racional::Racional(int num, int den) : numerador(num), denominador(den) { }
Racional operator+(const Racional& r1, const Racional& r2)
{
Racional res;
int num,den;
num = r1.obtenerNumerador() + r2.obtenerNumerador() ;
den = r1.obtenerDenominador() + r2.obtenerDenominador(); //Corregido
res.almacenarSuma(num, den);
return res;
}
int Racional::obtenerNumerador() const
{
return numerador;
}
int Racional::obtenerDenominador() const
{
return denominador;
}
void Racional::almacenarSuma(int num, int den)
{
numerador = num;
denominador = den;
}
If you would be so kind as to see with my own invented definition why it doesn't work I would appreciate it. I know that this solution is infinitely better, what it does is call the constructor passing directly as a parameter the sum of the numerators and on the other hand the sum of the denominators, but I would like to know why my version does not work.
Racional operator+(const Racional& r1, const Racional& r2)
{
return Complejo(r1.obtenerNumerador()+r2.obtenerNumerador(), r1.obtenerDenominador()+r2.obtenerDenominador());
}
Without further ado, very grateful , greetings to all.
EDIT: These are the errors I get from the compiler:
EDIT2:
main.cpp:9:10: error: invalid operands to binary expression ('Racional' and 'Racional')
res = p+q;
~^~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:743:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against
'Racional'
operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1163:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against
'Racional'
operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1576:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against
'Racional'
operator+(typename __wrap_iter<_Iter>::difference_type __n,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3698:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0,
type-parameter-0-1, type-parameter-0-2>' against 'Racional'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3711:1: note: candidate template ignored: could not match 'const _CharT *' against 'Racional'
operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3723:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0,
type-parameter-0-1, type-parameter-0-2>' against 'Racional'
operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3734:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0,
type-parameter-0-1, type-parameter-0-2>' against 'Racional'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3746:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0,
type-parameter-0-1, type-parameter-0-2>' against 'Racional'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
^
Limiting ourselves to the main problem: the
operator+
as an external function;Your approach is correct; You have missed a single detail : inform the compiler that this function exists :
If you don't, the compiler doesn't know that such a function exists, and will throw you an error every time you try to use it (as is your case).
By declaring the function, the compiler already knows what it is, what arguments it requires, and what data type it returns; to find it and link it, the linker takes care of it .
I don't know if it worked for you, but there is something very important to know. It is that the operators that belong to the group called binary arithmetic require two values, one from the object you invoke and the other from the object on the right side.
My first question is why did you do this?
Were you trying to update the object with the new value? remember that the addition was not for the object on the left side but for a new object. If you wanted to update the value of the object next to it by adding the value of the right side, you need to use the += operator .
I hope this has helped you. It is my first time responding on stackoverflow in Spanish.