关于用list做关于一元多项式相加的问题
程序代码:
//Polynomial.h 多项式头文件 #include<iostream> #include<list> #include<string> using namespace std; class Polynomial { public: Polynomial(float cf=0,int ep=0) :coef(cf),exp(ep){} Polynomial(Polynomial &p) :coef(p.coef),exp(p.exp){} float getCoef(); int getExp(); void showPolynomial() { cout << "(" << coef << "," << exp << ")" <<endl; } private: float coef; //系数 int exp; //指数 }; float Polynomial::getCoef() { return coef; } int Polynomial::getExp() { return exp; } //Polynomial.cpp #include<iostream> #include<list> #include"Polynomial.h" using namespace std; const float EPSINON = 0.00001; //初始化 template<class T> void initPolynomial(list<T> &p) { float coef; int exp; while(true) { cout << "Enter a coef or Enter 0 to end:"; cin >> coef; if((coef >= -EPSINON) && (coef <= EPSINON)) break; cout << "Enter a exp:"; cin >> exp; p.push_back(Polynomial(coef,exp)); } } //加法 //template<class T> //list<T> addPolynomial(list<T> &p1,list<T> &p2) //{ // auto iter(p1.begin().); 这里iter指向不到list的头指针 // iter. // //} int main() { list<Polynomial> polynomial_one; list<Polynomial> polynomial_two; cout << "输入第一个多项式:"<<endl; initPolynomial(polynomial_one); cout << "输入第二个多项式:"<<endl; initPolynomial(polynomial_two); /*addPolynomial(polynomial_one,polynomial_two);*/ cout << endl; /*auto iter(polynomial_one.begin()); 这里用iter取到list的指针 能指向getCoef()方法 while(iter != polynomial_one.end()) { iter++ ->showPolynomial(); }*/ system("pause"); } 我想用加法函数分别提取2个多项式的 系数和指数进行比较 相加但是list<T> addPolynomial(list<T> &p1,list<T> &p2) 中的iter指向不到,请问该用什么方法