gpt4 book ai didi

带有 ostream 运算符的 C++ 类可与 VS 一起使用,但无法使用 gcc 进行编译

转载 作者:行者123 更新时间:2023-11-30 05:02:24 26 4
gpt4 key购买 nike

我正在用 C++ 完成 CS2 的作业。我已经完成了我的代码,它在 Visual Studio 中按预期运行。然而,要提交代码,我们必须将其复制到我们的 unix 服务器并确保它仍然运行。

我无法让它运行,所以我想一定是我遗漏了 VS 正在纠正的错误?

我向我的教授展示了我的代码,他同意看起来一切都是正确的。

有人能帮忙吗?

理性.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

理性.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;
}

主要.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>] 

还有一整页的错误。

最佳答案

在 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 的“候选函数不可行:需要一个左值”,我找到了 https://stackoverflow.com/a/23209236

You need:

ostream& operator<< (ostream& out, Polynomial const& poly) {

This is because a temporary object cannot be bound to a non-const reference.

因此,创建 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

关于带有 ostream 运算符的 C++ 类可与 VS 一起使用,但无法使用 gcc 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49890554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com