2014-10-28 91 views
-9

的.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 

它說:」沒有匹配的構造函數的‘分數’初始化上的第一行cpp文件 我不明白這是什麼意思。

+1

在這種情況下,我們需要知道'Fraction.h'的內容,否則無法回答這個問題。 – hcs 2014-10-28 07:03:32

+0

在下次詢問之前,請仔細閱讀[這裏](http://stackoverflow.com/help/asking)的信息。 – 2014-10-28 07:55:01

回答

0

提供默認構造函數,如 分數() 分子= 0; denomenator0; denomp0; nump = 0; }

1

問題是你的分數構造函數需要2個參數。

Fraction (int a, int b); 

,你與無

Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction' 

您應該調用x和y與2個int參數或定義另一個構造不帶任何參數調用它。

+0

我試過這個,但後來這個錯誤消息來了架構x86_64的未定義符號: 「Fraction :: operator =(Fraction const&)」,引用來自: _main in calculator.o 「operator <<(std :: __ 1 :: basic_ostream >&,Fraction const&)「,引用來自: _main in calculator.o _main in useFraction.o ld:找不到架構x86_64的符號 clang:錯誤:鏈接器命令失敗,退出代碼1(使用-v查看調用) – warhol 2014-10-28 07:12:21

+0

您在Fraction類中的函數定義中缺少整個代碼負載(至少您沒有在此處發佈)。您需要在頭文件中內聯定義它們,或者在類定義之外定義它們,最好在名爲Fraction.cpp的文件中定義它們。 – 2014-10-28 07:18:53

+0

看看這裏http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/瞭解如何實施您的運營商。 – 2014-10-28 07:24:40

相關問題