大家好我是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
請拿出一個[MCVE(http://stackoverflow.com/help/mcve),而不是傾銷你的代碼。不過,我可以告訴你,這個錯誤[Clang非常明確地指出](http://coliru.stacked-crooked.com/a/3861b46e12f477ca),就在你有500個其他錯誤的中間(並且自從第一個不是很清楚,函數定義不應該在主體之前有分號,並且不應該有兩個返回類型)。 – chris 2014-11-05 01:54:50
你可能想讓你的重載流方法爲'public'而不是'private'。 'class'的默認訪問是'private'。 – 2014-11-05 02:01:02
@ThomasMatthews:流操作符不是方法,而是聲明爲「friend」。它們是無條件訪問的,但是'friend'聲明允許_them_訪問'fractionType'的'private'部分。 – 2014-11-05 02:03:53