在线等。错误提示“no operator">>" matches these operands”怎么修改?谢谢
是一个计算一元多项式的加法数组实现,一个数组放两个多项式的系数,另一个放指数,一个数组放系数相加后的结果。指数为0输入0。大部分函数中的cin cout都有这样的问题“no operator matches these operands”谢谢
[code=C/C++][/code]#include<iostream>
#include <stdlib.h>
#include <string>
#define degree 6
using namespace std;
class poly{
public:
void creat_coe(int);
void creat_exp(int);
void print();
void add();
};
poly coef[6], expo[6], result[6];
void poly::creat_coe(int n){
cout<<"Please enter two polynomial numbers' coefficients matching respective exponents ,if there is no such exponent in each term,please enter 0 "
<<"each number has max 3 terms"<<endl;
for(int i = 0; i < n; i++) {
cout<<"Please enter "<<i+1<<"coefficient:"; //循环输入第i个系数
cin >> coef[i]; //no operator"<<" matches these operands;
}
}
void poly::creat_exp(int n){
cout<<"Please enter two polynomial numbers' exponents matching respective coefficients from high order to low ,if there is no such exponent in each term,please enter 0 "
<<"each number has max 3 terms"<<endl;
for(int i = 0; i < n; i++) {
cout<<"Please enter "<<i+1<<"exponnet:"; //循环输入第i个指数
cin >> expo[i]; //no operator"<<" matches these operands;
}
}
void poly::add(){
for(int i=0; i<3; i++)
result[i]= coef[i] + coef[i+2];
}
void poly ::print(){
for(int i=0; i<6; i++)
cout << result[i] << expo[i]<<" "; // no operator << matches these operands
}
int main(){
poly number;
int n;
cout<<"Please enter terms of tow polynomials :" <<endl;
cin >> n;
number.creat_coe(n);
number.creat_exp(n);
number.add();
number.print();
return 0;
}
[解决办法]
[Quote=引用:]
是一个计算一元多项式的加法数组实现,一个数组放两个多项式的系数,另一个放指数,一个数组放系数相加后的结果。指数为0输入0。大部分函数中的cin cout都有这样的问题“no operator matches these operands”谢谢
[code=C/C++][/code]#include<iostream>
#include <stdlib.h>
#include <string>……
[/Quote]
expo[i];是一个poly类型 你又没有重载">>" 当然cin不进去
[解决办法]
因为expo[i];是自己定义的类型,你还是在该类型里面进行操作符重载<<和>>吧
[解决办法]
你的poly类一个属性都没有?不知道你的意图是什么,你的add里面还有加法,+也需要重载,给一个复数运算的重载的例子,你就懂了。
#include <iostream>using namespace std;class Complex {public: Complex (int r = 0, int i = 0) : m_r (r), m_i (i) {} /* Complex operator+ (const Complex& c) const { return Complex (m_r + c.m_r, m_i + c.m_i); } */ friend Complex operator+ (const Complex& c1, const Complex& c2) { return Complex (c1.m_r + c2.m_r, c1.m_i + c2.m_i); } Complex operator+ (int n) { return Complex (m_r + n, m_i); } Complex operator- (const Complex& c) const { return Complex (m_r - c.m_r, m_i - c.m_i); } /* Complex& operator+= (const Complex& c) {// return *this = *this + c; m_r += c.m_r; m_i += c.m_i; return *this; } */ friend Complex& operator+= (Complex& c1, const Complex& c2) { return c1 = c1 + c2; } Complex& operator-= (const Complex& c) { return *this = *this - c; } Complex operator- (void) { return Complex (-m_r, -m_i); } const Complex operator++ (int) { Complex c = *this; m_r++; m_i++; return c; } Complex& operator++ (void) { m_r++; m_i++; return *this; } const Complex operator-- (int) { Complex c = *this; m_r--; m_i--; return c; } Complex& operator-- (void) { m_r--; m_i--; return *this; }private: int m_r; int m_i; friend ostream& operator<< (ostream&, const Complex&); friend istream& operator>> (istream&, Complex&);};ostream& operator<< (ostream& os, const Complex& c) { os << "(" << c.m_r << "+" << c.m_i << "i)"; return os;}istream& operator>> (istream& is, Complex& c) { return is >> c.m_r >> c.m_i;}int main (void) { Complex c1 (3, 4); cout << c1 << endl; // operator<<(cout,c1) // operator<<(cout,c1).operator<<(endl); Complex c2 (5, 6);// cin >> c2; // operator>> (cin, c2) Complex c3 = c1 + c2; // c3 = c1.operator+ (c2) // c3 = operator+ (c1, c2) cout << c1 << '+' << c2 << '=' << c3 << endl; cout << c3 << '-' << c2 << '=' <<c3-c2<<endl; Complex c4 (c1); (c4 += c2) += c2; // c4.operator+= (c2); // operator+= (c4, c2); cout << c4 << endl; (c4 -= c2) -= c2; cout << c4 << endl; cout << -c4 << endl; int n = 10; cout << n++ << endl; // 10 cout << n << endl; // 11// (n++)++; int m = 10; cout << ++m << endl; // 11 cout << m << endl; // 11 ++++m; cout << m << endl; cout << c1++ << endl; // c1.operator++ (0) cout << c1 << endl;// c1++++; cout << ++c2 << endl; cout << c2 << endl; ++++c2; cout << c2 << endl; cout << c2+100 << endl; // c2.operator+(100) return 0;}