[英]when compiling my 3 files I received errors saying no match for 'operator='
我的代碼看起來以“a/b”形式接受用戶輸入並進行小數數學運算,然后輸出結果。我似乎得到的主要錯誤是“operator=”不匹配。 第 39 行和第 40 行給出了錯誤。感謝您的幫助。
我得到的錯誤顯示在這里:在此處輸入圖像描述
主文件
// Add appropriate headers
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include "rational7.h"
using namespace std;
int main()
{
// ToDo: declare three rational objects using the default constructor
Rational a, b, c;
int length;
char answer='Y';
// Main loop to read in rationals and compute the sum
do {
cout << "Enter the number of fractions to add/multiply: ";
cin >> length;
Rational *fractions = new Rational[length];
for (int i = 0; i < length; i++)
{
fractions[i].set (1,i+1);
fractions[i].output();
}
Rational r_sum, r_product(1);
//tempsum = fractions[0];
//tempproduct = fractions[0];
for (int i = 0; i < length; i++)
{
r_sum = sum(r_sum, fractions[i]); // test r.sum
r_product = product(r_product, fractions[i]); // test
}
//simplify
//cout << "Sum of fractions: " << r_sum.output() << endl;
//cout << "Prod of fractions: " << r_product.output() << endl;
// ToDo: use your input member function to read the first rational
cout << "Enter two rationals to compare ";
a.input();
b.input();
a.output(); // Debug line
b.output(); // Debug line
while (a.getDenominator() == 0 || b.getDenominator() == 0)
{
cout << "wrong input. \n";
cout << "Enter a rational (p/q): \n";
a.input();
b.input();
}
// cout << "Total number of objects created = " << << endl;
cout << "\nTry again (Y/N)?\n";
cin >> answer;
cin.ignore(256,'\n');
} while (answer == 'y' || answer == 'Y');
return 0;
}
理性7.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include "rational7.h"
using namespace std;
// ToDO: Implement your class member functions below.
/*
Rational::Rational()
{
numerator = 0;
denominator = 1;
}
Rational::Rational(int num)
{
numerator = num;
denominator = 1;
}
Rational::Rational(int num, int den)
{
Rational r;
numerator = num;
denominator = den;
}
*/
int Rational::counter = 0;
//Three constructors to one
Rational::Rational(int num, int den)
{
numerator = num;
denominator = den;
}
void sum(const Rational &a, const Rational &b)
//void Rational::sum(const Rational &a, const Rational &b)
{
Rational temp;
int num = 0;
int den = 0;
//num = (a.getNumerator()*b.getDenominator() + a.getDenominator()*b.getNumerator());
//den = (a.getDenominator()*b.getDenominator());
num = (a.numerator*b.denominator + a.denominator*b.numerator);
den = (a.denominator*b.denominator);
temp.numerator = num;
temp.denominator = den;
//return temp;
}
//void Rational::product(const Rational &a, const Rational &b)
void product(const Rational &a, const Rational &b)
{
Rational temp;
//int num = (a.getNumerator()*b.getNumerator());
//int den = (a.getDenominator()*b.getDenominator());
int num = (a.numerator*b.numerator);
int den = (a.denominator*b.denominator);
temp.numerator = num;
temp.denominator = den;
//return temp;
}
void Rational::input()
{
string in;
getline(cin, in);
//cin >> in;
// find the index position of /
int indx = in.find("/");
// seperate the numerator
numerator = atoi(in.substr(0, indx).c_str());
// seperate the denominator
denominator = atoi(in.substr(indx+1, in.length()).c_str());
//if(denominator != 0)
Rational(numerator, denominator);
}
void Rational::output()const
{
cout << numerator << "/" << denominator;
}
void Rational::set(int num, int den)
{
numerator = num;
denominator = den;
}
// Two getter functions
int Rational::getNumerator()const
{
return numerator;
}
int Rational::getDenominator()const
{
return denominator;
}
/*
void simplify(int num, int den)
{
gcd(num, den);
}
*/
/*
int gcd (int a, int b)
{
while (a!=0 && b!=0)
{
a = a % b;
if (a!=0)
b = b % a;
}
if (a==0)
return b;
if (b==0)
return a;
}
*/
/*
bool isEqual(const Rational &a, const Rational &b)
{
if (a.numerator * b.denominator == a.denominator * b.numerator)
{
return true;
}
else
{
return false;
}
}
*/
int Rational::getObjectCounter()
{
return counter;
}
理性7.h
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
using namespace std;
/* KEEP THIS COMMENT
* class Rational
* represents a Rational number. Remember rational means ratio-nal
* which means there is a numerator and denominator having
* integer values. Using good ADT techniques, we have made member
* variable private (also known as instance variables) and made member
* functions public.
*/
class Rational
{
private:
int numerator;
int denominator;
static int counter;
//void simplify(int num, int den);
public:
/*
// ToDo: Default Constructor
Rational();
// ToDo: Constructor that takes int numerator
Rational(int num);
// ToDo: Constructor that takes int numerator and int denominator
Rational(int num, int den);
*/
// Three constructors to one
Rational(int num = 0, int den = 1);
// ToDo: Member function to read a rational in the form: n/d
void input();
// ToDo: Member function to write a rational as n/d
void output()const;
// ToDo: declare an accessor function to get the numerator
int getNumerator()const;
// ToDo: declare an accessor function to get the denominator
int getDenominator()const;
// object counter 'global'
static int getObjectCounter();
// ToDo: delcare a function called Sum that takes two rational objects
// sets the current object to the sum of the given objects using the
// formula: a/b + c/d = ( a*d + b*c)/(b*sd)
friend void sum(const Rational &a, const Rational &b);
friend void product(const Rational &a, const Rational &b);
void set(int num, int den);
//bool isEqual(const Rational &a, const Rational &b);
};
編譯器會通知您它不知道您要調用什么 operator=()。 通常對於某些類型 C++ operator=
- 它是一個 function 被調用來分配用戶定義類型的值。 在您的情況下, Rational
- 是用戶定義的類型。
查看您的代碼 - 您聲明:
friend void sum(const Rational &a, const Rational &b);
然后將其稱為r_sum = sum(r_sum, fractions[i]); // test r.sum
r_sum = sum(r_sum, fractions[i]); // test r.sum
含義編譯器正在尋找可以接受 void 作為參數的Rational
的 function 或成員 function。 operator=(void)
- 這是因為在您的情況下sum
返回 void。
您可以自己定義這樣的運算符。 但我相信你真正希望它從 sum 返回一個Rational
值:
friend Rational sum(const Rational &a, const Rational &b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.