2014-11-05 168 views
0

大家好我是C++的新手,我在編譯我的代碼時遇到了一些錯誤。該計劃的重點是超載不同的運營商。我得到的錯誤我從未見過。任何指向我的問題可能會有所幫助。我究竟做錯了什麼?謝謝C++編譯錯誤

錯誤如下:

的main.cpp | 16 |錯誤: '操作符>>' 曖昧過載(操作數類型是 '的std :: istream的{又名性病:: basic_istream}' 和「 fractionType()」

的main.cpp | 19 |錯誤:對不明確的超負荷 '運營商>>'(操作數類型是 '的std :: istream的{又名性病:: basic_istream}' 和 'fractionType()'

main.cpp | 23 |錯誤:類型'fractionType()'和'fractionType()'到二元運算符+的無效操作數

main.cpp | 25 |錯誤:ISO C++禁止在減法中使用指針指向函數[-fpermissive]

main.cpp | 27 | error:類型'fractionType()'和'fractionType()'的操作數無效二進制 '操作符*'

的main.cpp | 29 |錯誤:類型的無效操作數的fractionType()'和 'fractionType()' 二進制 '運營商/'

這裏是我的代碼,

main.cpp

#include <iostream> 
#include <iomanip> 
#include "fractionType.h" 
using namespace std; 

int main() 
{ 
    fractionType num1(5, 6); 
    fractionType num2(); 
    fractionType num3(); 

    cout << fixed; 
    cout << showpoint; 
    cout << setprecision(2); 

    cout << "Line 7: Num1 = " << num1 << endl; 
    cout << "Line 8: Num2 = " << num2 << endl; 

    cout << "Line 9: Enter the fraction " 
     << "in the form a/b: "; 
    cin >> num2; 
    cout << endl; 

    cout << "Line 12: New value of num2 = " 
     << num2 << endl; 

    num3 = num1 + num2; 

    cout << "Line 14: Num3 = " << num3 << endl; 
    cout << "Line 15: " << num1 << " + " << num2 
     << " = " << num1 + num2 << endl; 
    cout << "Line 16: " << num1 << " * " << num2 
     << " = " << num1 * num2 << endl; 

    num3 = num1 - num2; 

    cout << "Line 18: Num3 = " << num3 << endl; 
    cout << "Line 19: " << num1 << " - " << num2 
     << " = " << num1 - num2 << endl; 
    cout << "Line 20: (" << num1 << ")/(" << num2 
     << ") = " << num1/num2 << endl; 

    return 0; 
} 

fractionOverloading.cpp

#include <iostream> 
#include <iomanip> 
#include "fractionType.h" 
using namespace std; 

//constructors 
fractionType::fractionType(const int& nu, const int& de) 
{ 
    a = nu; 
    if (de==0) 
    { 
     cout <<"\n\tInvalid denominator." 
      <<"default value considered for denominator."; 
     b = 1; 
    } 
    else 
     b = de; 
} 
//default Constructor 
fractionType::fractionType() 
{ 
    a = 0; 
    b = 1; 
} 
//copy Constructor 
fractionType::fractionType(const fractionType& rightFraction) 
{ 
    a = rightFraction.a; 
    b = rightFraction.b; 
} 
//destructor 
fractionType::~fractionType() 
{ 
} 
//overloading relational operators 
bool fractionType::operator == (const fractionType& rightFraction) const 
{ 
    return ((a == rightFraction.a)&& (b == rightFraction.b)); 
} 

bool fractionType::operator != (const fractionType& rightFraction) const 
{ 
    return ((a != rightFraction.a)|| (b != rightFraction.b)); 
} 

bool fractionType::operator < (const fractionType& rightFraction) const 
{ 
    return (a * rightFraction.b < b * rightFraction.a); 
} 

bool fractionType::operator <= (const fractionType& rightFraction) const 
{ 
    return (a * rightFraction.b <= b* rightFraction.a); 
} 

bool fractionType::operator > (const fractionType& rightFraction) const 
{ 
    return (a * rightFraction.b > b* rightFraction.a); 
} 

bool fractionType::operator >= (const fractionType& rightFraction) const 
{ 
    return (a * rightFraction.b >= b* rightFraction.a); 
} 

bool fractionType fractionType::operator + (const fractionType& rightFraction); 
{ 
    fractionType temp; 

    temp.a = (a* rightFraction.b)+(b* rightFraction.a) 
    temp.b = b * rightFraction.b; 
    return temp; 
} 

bool fractionType fractionType::operator - (const fractionType& rightFraction); 
{ 
    fractionType temp; 

    temp.a = (a* rightFraction.b)-(b* rightFraction.a) 
    temp.b = b * rightFraction.b; 
    return temp; 
} 

bool fractionType fractionType::operator * (const fractionType& rightFraction); 
{ 
    fractionType temp; 

    temp.a = a * rightFraction.a; 
    temp.b = b * rightFraction.b; 
    return temp; 
} 

bool fractionType fractionType::operator/(const fractionType& rightFraction); 
{ 
    fractionType temp; 
    //Checking to see if division is legal 
    if ((rightFraction.a == 0)|| (rightFraction.b==0)) 
    { 
     temp.a=0; 
     temp.b=1; 
     cout << "\n|tInvalid to perform division."; 
    } 
    else 
    { 
     temp.a = a * rightFraction.b; 
     temp.b = b * rightFractin.a; 
    } 
    return temp; 
} 

ostream& operator << (ostream& osObject, const fractionType& myFraction) 
{ 
    osObject << myFraction.a << "/" << myFraction.b; 
    return osObject; 
} 

istream& operator >> (istream& isObject, const fractionType& myFraction) 
{ 
    char ch; 
    isObject >> myFraction.a >> ch >> myFraction.b; 
    return isObject; 
} 

fractionType.h

#ifndef FRACTIONTYPE_H_INCLUDED 
#define FRACTIONTYPE_H_INCLUDED 
#include <iostream> 
using namespace std; 

class fractionType 
{ 
    friend ostream& operator << (ostream&, const fractionType&); 
    friend istream& operator >> (istream&, fractionType&); 

    public: 
    //overloading operators 
    const fractionType& operator=(const fractionType&); 

    //constructors 
    fractionType(); 
    fractionType(const fractionType&); 
    fractionType(const int&, const int&); 

    //destructor 
    ~fractionType(); 

    bool operator == (const fractionType&) const; 
    bool operator != (const fractionType&) const; 
    bool operator <= (const fractionType&) const; 
    bool operator < (const fractionType&) const; 
    bool operator >= (const fractionType&) const; 
    bool operator > (const fractionType&) const; 

    fractionType operator +(const fractionType&); 
    fractionType operator -(const fractionType&); 
    fractionType operator *(const fractionType&); 
    fractionType operator /(const fractionType&); 

private: 
    int a; 
    int b; 
}; 

#endif // FRACTIONTYPE_H_INCLUDED 
+0

請拿出一個[MCVE(http://stackoverflow.com/help/mcve),而不是傾銷你的代碼。不過,我可以告訴你,這個錯誤[Clang非常明確地指出](http://coliru.stacked-crooked.com/a/3861b46e12f477ca),就在你有500個其他錯誤的中間(並且自從第一個不是很清楚,函數定義不應該在主體之前有分號,並且不應該有兩個返回類型)。 – chris 2014-11-05 01:54:50

+0

你可能想讓你的重載流方法爲'public'而不是'private'。 'class'的默認訪問是'private'。 – 2014-11-05 02:01:02

+1

@ThomasMatthews:流操作符不是方法,而是聲明爲「friend」。它們是無條件訪問的,但是'friend'聲明允許_them_訪問'fractionType'的'private'部分。 – 2014-11-05 02:03:53

回答

1

這些函數聲明:

fractionType num2(); 
fractionType num3(); 

你大概的意思是:

fractionType num2; 
fractionType num3; 

它聲明瞭使用默認構造函數構造的變量。

最後四條錯誤消息來自試圖對函數指針進行算術運算。如果你看到這樣的錯誤信息,那麼你可能會成爲你陷入「煩人的解析」的受害者的線索,即當你打算聲明一個變量時實際上聲明瞭一個函數。

我認爲前兩個錯誤消息是一個編譯器錯誤?它應該是一個明確的轉換爲bool。也許編譯器有一個擴展名將函數指針轉換爲void *

+0

我試過了,我得到了'frationType :: fractionType()'的未定義引用的錯誤,但沒有給出錯誤的行號。 – MPNation 2014-11-05 02:17:37

+0

@KennyWatts這完全是一個不同的錯誤(你現在從原來的錯誤轉移到了新的錯誤)。 [見這裏](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400)for完整的解釋;簡短的版本是,你實際上並沒有告訴你的編譯器使用你剛纔編譯的所有文件。如果您仍然遇到問題,請發佈新問題。 – 2014-11-05 02:22:30

+0

明白了!謝謝。 – MPNation 2014-11-05 02:27:41

1

你的問題是被稱爲Most Vexing Parse一個可怕的問題:你的「對象」 num2num2對象,但函數聲明!當某件事可以被視爲一種聲明而不是一種聲明時,它被認爲是一種聲明。

的可行修補程序是

fractionType num1;     // you should have a default constructor initializing members, though 
fractionType num2{};    // this does the Right Thing but requires C++11 
fractionType num3 = factionType(); // the Right Thing but requires a copy/move constructor 
+0

以及其他問題:'istream&operator >>(istream&isObject,const fractionType&myFraction)''可能不應該有'const'參數。 – 2014-11-05 02:08:33