.cpp
//
// calculator.cpp
//
#include "Fraction.h"
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction'
char op;
try
{
cin >> x;
cin >> op;
while ( cin && ( op == '+' || op == '-' ) )
{
cin >> y;
if ( op == '+' )
x = x + y;
else
x = x - y;
cin >> op;
}
cout << x << endl;
}
catch ( invalid_argument& e )
{
cout << "Error: " << e.what() << endl;
}
}
.h
#ifndef Fraction_Calculator_Fraction_h
#define Fraction_Calculator_Fraction_h
#include<iostream>
#include<cstdlib>
//Fraction class definition
class Fraction
{
public:
Fraction (int a, int b);
int fraction(int a, int b);
void set(int, int);
int get_numerator(void);
int get_denomenator(void);
int find_gcd (int n1, int n2);
void reduce_fraction(int nump, int denomp);
Fraction& operator+(const Fraction& n);
Fraction& operator-(const Fraction& n);
friend std::ostream& operator<<(std::ostream &os, const Fraction& n);
friend std::istream& operator>>(std::istream &is, Fraction& n);
Fraction& operator= (const Fraction& n);
int denom;
int numera;
private:
int numerator;
int denomenator;
int denomp;
int nump;
};
#endif
它说&#34;没有匹配的构造函数用于初始化&#39;分数&#39;在cpp文件的第一行 我不明白这意味着什么。
答案 0 :(得分:1)
问题是你的Fraction构造函数需要2个参数。
Fraction (int a, int b);
并且您在没有
的情况下调用它Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction'
您应该使用2个int参数调用x和y,或者定义另一个不带参数的构造函数。
答案 1 :(得分:0)
提供默认构造函数 分数() { 分子= 0; denomenator0; denomp0; NUMP = 0; }