2015-11-13 6 views
-2

так что я пытаюсь получить это работать, но я получаюПочему я получаю сообщение об ошибке: не жизнеспособны перегружен «=»

result = f1.AddedTo(f2); //a class binary operation - a value-returning "observer" function 
~~~~~~^~~~~~~~~~~~~~~ 
note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'int' to 'const fraction' for 1st argument 
class fraction 
    ^

Код:

//client.cpp 
#include <iostream> 
#include "fraction.h" 
using namespace std; 

int main() 
{ 
fraction f1(9,8); //calling a parameterized class constructor 
fraction f2(2,3); 
fraction result; //calling a default class constructor 
const fraction f3(12, 8); 
const fraction f4(202, 303); 
fraction f5,f6; 

cout<<"C++ CLASS MULTI-FILE PROJECT"<<endl; 
cout<<"Client.cpp - testing a fraction class implementation\n"; 
cout<<"----------------------------------------------------\n\n"; 

cout << "The result object starts off at "; 
result.showFraction(); //calling a void "observer" function 
cout << endl; 


cout<<"\nArithmetic operations with fraction objects stored in the results  class object\n"; 
cout<<"------------------------------------------------------------------------------\n\n"; 

cout << "The sum of "; 
f1.showFraction(); 
cout << " and "; 
f2.showFraction(); 
cout << " is "; 
result = f1.AddedTo(f2); //a class binary operation - a value-returning    "observer" function 
result.showFraction(); 
cout << endl; 

cout << "The difference of "; 
f1.showFraction(); 
cout << " and "; 
f2.showFraction(); 
cout << " is "; 
result = f1.Subtract(f2); //a class binary operation - a value-returning  "observer" function 
result.showFraction(); 
cout << endl; 

cout << "The product of "; 
f1.showFraction(); 
cout << " and "; 
f2.showFraction(); 
cout << " is "; 
result = f1.MultipliedBy(f2); //a class binary operation - a value-returning  "observer" function 
result.showFraction(); 
cout << endl; 

result = f3.DividedBy(f4); //a class binary operation - a value-returning  "observer" function 
cout << "The quotient of "; 
f3.showFraction(); 
cout << " and "; 
f4.showFraction(); 
cout << " is "; 
result.showFraction(); 
cout << endl; 

cout<<"\nInput and Output operations for two new class objects\n"; 
cout<<"-----------------------------------------------------\n\n"; 

f5.getFraction(); //a class input operation - a transformer or setter function 
f5.showFraction(); //a class output operation - an observer or getter  function 
cout<<endl<<endl; 
f6.getFraction(); 
f6.showFraction(); 
cout<<endl<<endl; 

cout<<"\nA Boolean operation comparing two class objects\n"; 
cout<<"------------------------------------\n\n"; 

if (f5.isGreaterThan(f6)){ //a class relational expression - boolean   operation/function 
f5.showFraction(); 
cout <<" is greater than "; 
f6.showFraction(); 
cout<<endl; 
} else { 
f5.showFraction(); 
cout <<" is less than "; 
f6.showFraction(); 
cout<<endl; 
} 

cout<<"\n---------------------------------------------------------\n"; 
cout<<"\nFraction class implementation test now successfully concluded\n"; 

// system ("PAUSE"); 

return 0; 
} 

//fraction.cpp 
#include <iostream> 
#include "fraction.h" 

using namespace std; 

fraction::fraction() 
    { 
     numerator = 0; 
     denominator = 1; 
    } 

//--------------------------------------------------------- 

fraction::fraction(int n, int d) 
    { 
     numerator = n; 
     if (d==0) 
     { 
      cout << "The denominator can not equal zero. Please try again." << endl; 
      exit(0); 
     } 
     else 
      denominator = d; 
    } 

//--------------------------------------------------------- 

void fraction::getFraction() 
{ 
    cout << "Plese enter the numerator "; 
    cin >> n; 
    cout << "Plese enter the denominator "; 
    cin >> d; 


} 

//--------------------------------------------------------- 


void fraction::showFraction() const 
{ 
    cout << numerator << '/' << denominator; 
} 

//--------------------------------------------------------- 

    // I got the GCD algorithm from the following source: 
    // Source C#: http://www.ww.functionx.com/csharp2/examples/gcd.htm 
    int gcd(int n, int d) 
    { 
     int remainder; 
     while (d != 0) 
     { 
      remainder = n % d; 
      n = d; 
      d = remainder; 
     } 
     return n; 
    } 

//--------------------------------------------------------- 


int fraction::AddedTo(fractionotherFraction) 
    { 
     n =  numerator*otherFraction.denominator+otherFraction.numerator*denominator; 
     d = denominator*otherFraction.denominator; 
     return Fraction(n/gcd(n,d),d/gcd(n,d)); 
    } 

//--------------------------------------------------------- 


int fraction::Subtract(fraction otherFraction) 
    { 
     n = numerator*otherFraction.denominator- otherFraction.numerator*denominator; 
     d = denominator*otherFraction.denominator; 
     return Fraction(n/gcd(n,d),d/gcd(n,d)); 
    } 

//--------------------------------------------------------- 


int fraction::MultipliedBy(fraction otherFraction) 
    { 
     n = numerator*otherFraction.numerator; 
     d = denominator*otherFraction.denominator; 
     return Fraction(n/gcd(n,d),d/gcd(n,d)); 
    } 

//--------------------------------------------------------- 

int fraction::DividedBy(fraction otherFraction) 
    { 
     n = numerator*otherFraction.denominator; 
     d = denominator*otherFraction.numerator; 
     return Fraction(n/gcd(n,d),d/gcd(n,d)); 
    } 

//--------------------------------------------------------- 

    bool fraction::isGreaterThan(fraction otherFraction) 
{ 

} 

//fraction.h 
#include <iostream> 
using namespace std; 

class fraction 
{ 
public: 

    fraction(); 
    fraction(int,int); 
    void getFraction(); 
    void showFraction() const; 
    int AddedTo(fraction); 
    int Subtract(fraction); 
    int MultipliedBy(fraction); 
    int DividedBy(fraction) const; 
    bool isGreaterThan(fraction); 

private: 
    int numerator; 
    int denominator; 

}; 
+2

_Why_? Я дам вам знать, как только я прочитаю ваши 400 строк кода. Вы можете сделать это проще, предоставив [минимальный, полный и проверяемый пример] (http://stackoverflow.com/help/mcve). Тем не менее, как удар в темноте: потому что ваш класс 'fraction' ** не имеет ** перегруженного' operator = ', поэтому компилятор не знает, как вы ожидаете добавить' fraction' и 'int' (возвращенные 'AddedTo') вместе. Вам нужно определить пользовательский 'operator =' – Tas

+0

. Почему «AddedTo», «Subtract» и т. Д. Все имеют 'int' как возвращаемый тип, но' return Fraction (...); '? Что такое «Фракция»? Ваш тип - 'fraction'. – chris

ответ

0

У вас есть fraction класс и два fraction s: result и f1. Вы звоните fraction::AddedTo по телефону f1 и пытаетесь присвоить результат (int) result; однако компилятор не знает, как назначить один int вашему fraction.

Для этого вам нужно overload operator= для fraction:

fraction& operator=(int n) 
{ 
    numerator = denominator = n; // or however you wish to handle this 
    return *this; 
} 

По умолчанию компилятор сможет добавить два fraction S вместе, потому что это только предположения, что вы хотите сделать следующее:

fraction& operator=(const fraction& f) 
{ 
    numerator = f.numerator; 
    denominator = f.denominator; 
    return *this; 
} 

Но для всего остального вам необходимо перегрузить операторов.

Смежные вопросы