0

This may seem like a really dumb question, but I've been researching and taking examples for the last hour and have yet to find the explanation, so I might as well ask here while I keep searching.

Why does this work and compile properly:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
using namespace std;

class Fraccao{
    
   
    int denominador;
    
    
public:
     int numerador;
    Fraccao(int N = 0, int D = 1);
    void ChangeND(int N, int D);
    Fraccao operator*(Fraccao const &Frac){
        
        Fraccao FTemp;
        FTemp.numerador = this->numerador * Frac.numerador;
        FTemp.denominador = this->denominador * Frac.denominador;
        return FTemp;
        
    }
    
};

int main(){
    
    Fraccao F1(1, 2);
    Fraccao F2(3);
    Fraccao F3(3, 4);
    
    F1 = F2*F3;
    cout << F1.numerador;
    
}

Fraccao::Fraccao(int N, int D){
    
    numerador = N;
    if(D > 0){
        denominador = D;
    }else{
        denominador = 1;
    }
    cout << "Fraccao " << numerador << "/" << denominador << " criada." << endl;
}

void Fraccao::ChangeND(int N, int D){
    
    numerador = N;
    if(D > 0){
        denominador = D;
        
    }else{
        denominador = 1;
    }
    
}

But this doesn't:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
using namespace std;


class Fraccao{
    
   
    int denominador;
    
    
public:
     int numerador;
    Fraccao(int N = 0, int D = 1);
    void ChangeND(int N, int D);
    Fraccao operator*(Fraccao const &Frac);
    
};

int main(){
    
    Fraccao F1(1, 2);
    Fraccao F2(3);
    Fraccao F3(3, 4);
    
    F1 = F2*F3;
    cout << F1.numerador;
    
}

Fraccao::Fraccao(int N, int D){
    
    numerador = N;
    if(D > 0){
        denominador = D;
    }else{
        denominador = 1;
    }
    cout << "Fraccao " << numerador << "/" << denominador << " criada." << endl;
}

void Fraccao::ChangeND(int N, int D){
    
    numerador = N;
    if(D > 0){
        denominador = D;
        
    }else{
        denominador = 1;
    }
    
}

Fraccao::operator *(const Fraccao& Frac){
        
        Fraccao FTemp;
        FTemp.numerador = this->numerador * Frac.numerador;
        FTemp.denominador = this->denominador * Frac.denominador;
        return FTemp;
        
    }

The problem I appear to be getting on the second iteration of code is that: If I move the operator overload to outside the class it fails to work despite the prototype.

user215272
  • 11
  • 1
  • 3
  • You are missing the return type in your operator definition: ```Fraccao::operator *(const Fraccao& Frac){``` should be ```Fraccao Fraccao::operator *(const Fraccao& Frac){``` – Stefano Buora Jan 05 '21 at 13:40
  • Like this `Fraccao Fraccao::operator *(const Fraccao& Frac){` – john Jan 05 '21 at 13:40
  • But it's better to define `operator*` outside of the class `Fraccao operator*(const Fraccao& x, const Fraccao& y){`, see [here](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – john Jan 05 '21 at 13:41
  • @StefanoBuora OMG thank you so much, I actually tried that but I did it like Fraccao & Fraccao::operator*(const Fraccao& Frac), so I guess my mistake on adding the "&" messed it up. – user215272 Jan 05 '21 at 13:43
  • @john thank you so much, I actually tried that but like I pointed to someone else I mistaken myself by adding Fraccao & Fraccao. – user215272 Jan 05 '21 at 13:44
  • @john I'm assuming you're talking about operator overloading globally and then make it friend? Still learning this part of programming, I'll look into your link, thanks. – user215272 Jan 05 '21 at 13:46
  • @user215272 Yes that's right. If you make operator* part of a class then (in some circumstances) the compiler will treat the left and right hand sides differently. But if you make it global they always get treated the same. – john Jan 05 '21 at 13:49

1 Answers1

1

You are missing the return type in your operator definition:

Fraccao::operator *(const Fraccao& Frac){ 

should be

Fraccao Fraccao::operator *(const Fraccao& Frac){
Stefano Buora
  • 1,052
  • 6
  • 12