0

Im having problems implementing operator* in a class with 2 variables, a float and an int, of type float*x^int

When i try to * 2 objects or a float and an object the program stops and ends with an error.

Here is all the code im using, i appreciate any help.

Main file:

#include <iostream>
#include "usuario.h"
usuario& operator*(usuario& u1,usuario& u2);
usuario& operator*(usuario& u1,float num);

int main()
{
    usuario u1;
    usuario u2;
    std::cout<<"Monomio 1:";
    std::cin>>u1;
    std::cout<<std::endl<<"Monomio 2:";
    std::cin>>u2;
    float alfa = 3.2;
    usuario u3 = u1*u2;
    usuario u4 = u1;
    std::cout<<u1<<"*"<<u2<<"="<<u3<<std::endl;
    std::cout<<u1<<"*"<<alfa<<"="<<u4;

    return 0;
}

usuario& operator*(usuario& u1,usuario u2)
{
    u1*=u2;
    return u1;

}
usuario& operator*(usuario& u1,float num)
{
    usuario u2(num,0);
    u1*=u2;
    return u1;
}

Class file:

#ifndef USUARIO_H
#define USUARIO_H
#include <iostream>

class usuario
{
    public:
        usuario();
        usuario(float numero, int exponente);
        usuario(const usuario& u);
        usuario& operator=(const usuario& u);
        usuario& operator*=(const usuario& u);
        float num() const;
         int exp() const ;




    private:
        float numero;
         int exponente;

};
std::istream& operator>>(std::istream& in, usuario& u);
std::ostream& operator<<(std::ostream& out,const usuario& u);



#endif // USUARIO_H

Implementation file:

#include "usuario.h"

usuario::usuario():numero(1),exponente(1)
{

}
usuario::usuario(float numero, int exponente):numero(numero),exponente(exponente)
{

}
usuario::usuario(const usuario& u):numero(u.numero),exponente(u.exponente)
{

}
usuario& usuario::operator*=(const usuario& u)
{
    numero*=u.numero;
    exponente+=u.exponente;
    return *this;
}
std::istream& operator>>(std::istream& in ,usuario& u)
{
    char ch;
    float numero;
    int exponente;
    in>>numero;
    in>>ch;
    in>>ch;
    in>>exponente;
    u = usuario(numero,exponente);
    return in;

}
std::ostream& operator<<(std::ostream& out,const usuario& u)
{
    out<<u.num();
    out<<"x";
    out<<"^";
    out<<u.exp();
    return out;
}
float usuario::num()const
{
    return numero;
}
 int usuario::exp()const
{
    return exponente;
}

usuario& usuario::operator=(const usuario& u)
{
    numero = u.numero;
    exponente = u.exponente;
    return *this;
}

Btw, the *operators are implemented and defined in the main file, im trying different thing but same happens, i would appreciate any help. Thanks

Im trying to implement the *operator for a class, but an error occurs

  • 1
    `usuario& operator*(usuario& u1,usuario u2)` Bad plan. Would you expect 3*2 to result in the 3 turning into 6? That's what you've written. Give [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) a read-thorough for a discussion on how to do operators correctly. Might even have the answer you're looking for in there. – user4581301 Nov 23 '22 at 00:09
  • 1
    Beside the correct way of implementing math operators mentioned by @user4581301, *'but an error occurs'* -- Does the error message turns out saying something like: *'underlined reference to ...'* or *'unresolved external symbol ...'*? That's a clue on what is required to make it run. – Ranoiaetep Nov 23 '22 at 00:28
  • Looking closer, `usuario& operator*(usuario& u1,usuario& u2);` declares a function that takes two references to `usuario`. However the function that was declared, `usuario& operator*(usuario& u1,usuario u2)` takes one reference to `usuario` and one `usuario`. They do not match. When I get an error like this and I know I wrote the function, I place the declaration on top of the definition and look for a mismatch. Tside by side, the missing `&` really stands out. Fix this and the code will link, but you'll still have to deal with the logic error I point out above. – user4581301 Nov 23 '22 at 00:29
  • Alredy fixed it. I created a new dynamic object and operated with it, instead of using the reference – Alvaro Suarez Menendez Nov 23 '22 at 00:32
  • No need for any dynamic objects here. Automatic allocations are just fine. – user4581301 Nov 23 '22 at 00:43

0 Answers0