大家好我不熟悉C ++,在编写类代码时遇到一些错误。该计划的重点是使不同的运营商超负荷。我得到的错误,我以前从未见过。任何指向我的问题的指针都会有所帮助。我究竟做错了什么?谢谢
错误如下:
main.cpp | 16 |错误:'operator>>'的模糊重载(操作数类型是'std :: istream {aka std :: basic_istream}'和'fractionType()'
main.cpp | 19 |错误:'operator>>'的模糊重载(操作数类型是'std :: istream {aka std :: basic_istream}'和'fractionType()'
main.cpp | 23 | error:类型'fractionType()'和'fractionType()'到二进制'operator +'的无效操作数
main.cpp | 25 |错误:ISO C ++禁止在减法中使用指向函数的指针[-fpermissive]
main.cpp | 27 | error:类型'fractionType()'和'fractionType()'到二进制'operator *'的无效操作数
main.cpp | 29 | error:类型'fractionType()'和'fractionType()'到二进制'operator /'的无效操作数
这是我的代码,
的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 :(得分:1)
这些是函数声明:
fractionType num2();
fractionType num3();
你可能意味着:
fractionType num2;
fractionType num3;
声明变量,使用默认构造函数构造。
最后四条错误消息来自于尝试对函数指针进行算术运算。如果你看到这种错误信息,那么你就可以成为一个线索,让你成为&#34;烦恼的解析&#34;的受害者,即当你想要声明一个变量时实际宣布一个函数。
我认为前两个错误消息是编译器错误?它应该是bool
的明确转换。也许编译器有一个扩展名,可以将函数指针转换为void *
。
答案 1 :(得分:1)
你的问题是一个可怕的问题,称为Most Vexing Parse:你的&#34;对象&#34; num2
和num2
不是对象,但函数声明!当某些事物可以被视为声明而不是声明时,它被视为声明。
可行的修复
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