why this dont work? please help me

This is a discussion on why this dont work? please help me within the C++ Programming forums, part of the General Programming Boards category; hi, I'm studing the + and the = operator overloading to make something like obj1 = obj2 + obj3, but ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    113

    why this dont work? please help me

    hi,
    I'm studing the + and the = operator overloading to make something like obj1 = obj2 + obj3, but my code wont run here is it:
    please see the main program at the botton

    Code:
    //class definition
    class NRacionales{
       friend ostream &operator<<(ostream &, const NRacionales   &);
       public:   
          
          NRacionales(int = 1, int = 1);
          NRacionales operator+(const NRacionales &);
          NRacionales &operator=(NRacionales &);
          .
          .
       private:
          int numerador;
          int denominador;
             
    }
    
    //function definitions
    //definition of << overloading not need it here
    
    NRacionales::NRacionales(int num, int den)
    {
         numerador = num;
         denominador = den;
    }
    
    //+ operator overloaded
    NRacionales NRacionales::operator+(const NRacionales &derecha)
    {
          //returns an instance of the class
           return NRacionales((numerador * derecha.denominador) + 
    	 (derecha.numerador * denominador),			  (denominador * derecha.denominador));
    }
    
    //= operator overloaded
    NRacionales &NRacionales::operator=(NRacionales &derecha)
    {
           numerador = derecha.numerador;   
          denominador = derecha.denominador;
          return *this;
    }
    
    //main
    int main()
    {
            NRacionales n1(8,4);
            NRacionales n2(9,3);
            NRacionales n4;
          
            //this block of code works fine
            cout << "N1= " << n1 << endl;
            cout << "N2= " << n2 << endl; 
            cout << "N4= " << n4 << endl;
       
            //this line of code works fine
            cout << "\nn1 + n2 = " << n1 + n2 << endl;
       
          //this dont work , why? 
          n4 = n1 + n2;
          cout << "\nn4 = n1 + n2 = " << n4 << endl;
          system("pause");
          return 0;
    }
    
    ok here is the error message the compiler generates(dev c++):
    
    28 C:\programas c++\ejer8_17\main.cpp
    no match for `NRacionales& = NRacionales' operator
    
    28 C:\programas c++\ejer8_17\fracciones.h
    candidates are: NRacionales&  NRacionales::operator=(NRacionales&)
    please excuse my poor english

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    do you have a default constructor? the line:

    NRacionales n4;

    attempts to call the default constructor. Since you already have a non-default non-copy constructor the compiler won't provide a default constructor for you.

    Since numerado and denominador are private members of NRacionales, when trying to access them for derecha you will need accessor functions as well.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    i have the copy constructor:
    NRacionales(const NRacionales &);

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    how about a default constructor:

    NRacionales() : numerador(0), denominador(0) {}

    or some such beast?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21