我正在为C ++中的CS2工作。我已经完成了我的代码,它在Visual Studio中按预期运行。但是要提交代码,我们必须将其复制到我们的unix服务器并确保它仍然运行。
我无法让它运行所以我想我肯定错过了VS正在纠正的错误?
我向我的教授展示了我的代码,他同意看起来一切都是正确的。
有人可以帮忙吗?
rational.h
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
#include <ostream>
#include <cmath>
#include <cstdlib>
using namespace std;
class rational
{
friend ostream& operator<<(ostream &, rational&);
friend istream& operator>>(istream &, rational&);
public:
rational operator+(const rational &)const;
rational operator-(const rational &)const;
bool rational::operator>(const rational &r2);
rational(int n = 0, int d = 1);
rational add(const rational &r2) const;
void add(const rational &r1, const rational &r2);
rational subtract(const rational &r2) const;
void subtract(const rational &r1, const rational &r2);
rational multiply(const rational &r2) const;
rational divide(const rational &r2) const;
int compare(const rational &r2) const;
private:
int num; // numerator
int denom; // denominator
};
#endif
rational.cpp
#include "rational.h"
using namespace std;
ostream& operator<<(ostream &out, rational &robj)
{
out << robj.num << "/" << robj.denom;
return out;
}
istream& operator>>(istream &in, rational &obj)
{
cout << "Enter values for the numerator and denominator of a rational number: ";
in >> obj.num >> obj.denom;
return in;
}
rational::rational(int n, int d)
{
num = n;
denom = d;
}
rational rational::operator+(const rational &r2) const
{
rational sum;
sum.denom = (*this).denom * r2.denom;
sum.num = ((*this).num * r2.denom) + (r2.num * (*this).denom);
return sum;
}
rational rational::operator-(const rational &r2) const
{
rational diff;
diff.denom = (*this).denom * r2.denom;
diff.num = ((*this).num * r2.denom) - (r2.num * (*this).denom);
return diff;
}
bool rational::operator>(const rational &r2)
{
double x1, x2;
x1 = double((*this).num) / double((*this).denom);
x2 = double(r2.num) / double(r2.denom);
return (x1 > x2);
}
rational rational::add(const rational &r2) const
{
rational sum;
sum.denom = (*this).denom * r2.denom;
sum.num = ((*this).num * r2.denom) + (r2.num * (*this).denom);
return sum;
}
void rational::add(const rational &r1, const rational &r2)
{
(*this).denom = r1.denom * r2.denom;
(*this).num = (r1.num * r2.denom) + (r2.num * r1.denom);
}
rational rational::subtract(const rational &r2) const
{
rational diff;
diff.denom = (*this).denom * r2.denom;
diff.num = ((*this).num * r2.denom) - (r2.num * (*this).denom);
return diff;
}
void rational::subtract(const rational &r1, const rational &r2)
{
(*this).denom = r1.denom * r2.denom;
(*this).num = (r1.num * r2.denom) - (r2.num * r1.denom);
}
rational rational::multiply(const rational &r2) const
{
rational multi;
multi.denom = (*this).denom * r2.denom;
multi.num = (*this).num * r2.num;
return multi;
}
rational rational::divide(const rational &r2) const
{
rational div;
div.denom = (*this).denom * r2.num;
div.num = (*this).num * r2.denom;
return div;
}
int rational::compare(const rational &r2) const
{
if ((*this).denom == r2.denom && (*this).num == r2.num)
return 0;
else if (double((*this).num) / double((*this).denom) > double(r2.num) / double(r2.denom))
return 1;
else
return -1;
}
的main.cpp
#include "rational.h"
using namespace std;
int main()
{
rational r1(1,4), r2(1,3),r3;
cout << "r1 is initialized by the 2nd constructor: r1 = " << r1 << endl;
cout << "r2 is initialized by the 2nd constructor: r2 = " << r2 << endl << endl;
cout << "Testing the comapre() member function, found:" << endl << "\t";
int compare = r1.compare(r2);
switch (compare){
case 0:
cout << r1 << " is equal to " << r2;
break;
case 1:
cout << r1 << " is greater than " << r2;
break;
case -1:
cout << r1 << " is less than " << r2;
break;
}
cout << endl << endl;
cout << "Testing the four arithmetic member functions:" << endl;
r3.add(r1, r2);
cout << "\tr1 + r2 = " << r1 << " + " << r2 << " = " << r3 << endl;
r3.subtract(r1, r2);
cout << "\tr1 - r2 = " << r1 << " - " << r2 << " = " << r3 << endl;
cout << "\tr1 * r2 = " << r1 << " * " << r2 << " = " << r1.multiply(r2) << endl;
cout << "\tr1 / r2 = " << r1 << " / " << r2 << " = " << r1.divide(r2) << endl;
}
编译命令g++ -c main.cpp rational.cpp
我收到错误
no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>]
另一整页错误。
答案 0 :(得分:1)
使用Unix(Linux)上的gcc和clang / llvm编译器,我有关于两个运算符定义的rational::
前缀的错误消息(对于现代g ++和clang ++,有颜色编码,红色表示错误):
$ gcc *.cpp -o a -w
In file included from main.cpp:1:0:
rational.h:20:10: error: extra qualification ‘rational::’ on member ‘operator>’ [-fpermissive]
bool rational::operator>(const rational &r2);
^~~~~~~~
在类声明中声明的运算符不需要这些前缀,只需在类中使用bool operator>(const rational &r2);
。
其他错误会为r1.multiply(r2)
和r1.divide(r2)
的两行生成大量文本,并且真正原因不容易找到。那是:
$ g++ main.cpp rational.cpp -w -o program
main.cpp: In function ‘int main()’:
main.cpp:33:58: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘rational’)
cout << "\tr1 * r2 = " << r1 << " * " << r2 << " = " << r1.multiply(r2) << endl;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
....skip... no known conversion for ...
In file included from main.cpp:1:0:
rational.h:13:21: note: candidate: std::ostream& operator<<(std::ostream&, rational&) <near match>
friend ostream& operator<<(ostream &, rational&);
^~~~~~~~
rational.h:13:21: note: conversion of argument 2 would be ill-formed:
main.cpp:33:72: error: invalid initialization of non-const reference of type ‘rational&’ from an rvalue of type ‘rational’
cout << "\tr1 * r2 = " << r1 << " * " << r2 << " = " << r1.multiply(r2) << endl;
~~~~~~~~~~~^~~~
main.cpp:34:58: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘rational’)
cout << "\tr1 / r2 = " << r1 << " / " << r2 << " = " << r1.divide(r2) << endl;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
所以,编译器不能使用你的输出函数变体...为第二个参数ostream搜索“候选函数不可行:期望一个l值”我发现https://stackoverflow.com/a/23209236
你需要:
ostream& operator<< (ostream& out, Polynomial const& poly) {
这是因为临时对象不能绑定到非const引用。
因此,创建ostream& operator<<(ostream &out, rational &robj)
方法的变体以接受const引用,如ostream& operator<<(ostream &out, rational const &robj)
,当您尝试在具有multipy和divide的行中输出临时有理对象时。
我的补丁:
diff -ur orig/rational.cpp new/rational.cpp
--- orig/rational.cpp 2018-04-18 06:30:14.734426770 +0300
+++ new/rational.cpp 2018-04-18 06:46:49.839818729 +0300
@@ -1,10 +1,15 @@
#include "rational.h"
using namespace std;
-
ostream& operator<<(ostream &out, rational &robj)
{
out << robj.num << "/" << robj.denom;
return out;
+}
+
+ostream& operator<<(ostream &out, rational const &robj)
+{
+ out << robj.num << "/" << robj.denom;
+ return out;
}
istream& operator>>(istream &in, rational &obj)
{
diff -ur orig/rational.h new/rational.h
--- orig/rational.h 2018-04-18 06:20:14.431675001 +0300
+++ new/rational.h 2018-04-18 06:47:00.691799442 +0300
@@ -11,13 +11,14 @@
{
friend ostream& operator<<(ostream &, rational&);
+ friend ostream& operator<<(ostream &, rational const&);
friend istream& operator>>(istream &, rational&);
public:
rational operator+(const rational &)const;
rational operator-(const rational &)const;
- bool rational::operator>(const rational &r2);
+ bool operator>(const rational &r2);
rational(int n = 0, int d = 1);
输出:
$ g++ main.cpp rational.cpp -o program
$ ./program
r1 is initialized by the 2nd constructor: r1 = 1/4
r2 is initialized by the 2nd constructor: r2 = 1/3
Testing the comapre() member function, found:
1/4 is less than 1/3
Testing the four arithmetic member functions:
r1 + r2 = 1/4 + 1/3 = 7/12
r1 - r2 = 1/4 - 1/3 = -1/12
r1 * r2 = 1/4 * 1/3 = 1/12
r1 / r2 = 1/4 / 1/3 = 3/4
$ clang++ main.cpp rational.cpp -o program
$ ./program
r1 is initialized by the 2nd constructor: r1 = 1/4
r2 is initialized by the 2nd constructor: r2 = 1/3
Testing the comapre() member function, found:
1/4 is less than 1/3
Testing the four arithmetic member functions:
r1 + r2 = 1/4 + 1/3 = 7/12
r1 - r2 = 1/4 - 1/3 = -1/12
r1 * r2 = 1/4 * 1/3 = 1/12
r1 / r2 = 1/4 / 1/3 = 3/4