C++ c+中的模板错误+; #包括 使用名称空间std; 模板 结构术语 { T系数; 国际贸易; }; 模板 类多项式 { 公众: 类术语*术语数组; 国际能力; 国际术语; 多项式(整数大小); ~多项式() { 删除[]TERM数组; } 多项式&Add(多项式b); 多项式&Mul(多项式b); void NewTerm(const float theCoef,const int theExp); void show(); }; 模板 多项式::多项式(int-size)//在没有参数列表的情况下使用模板名“多项式”无效 { 术语=0; 此->termarray=新的T[大小]; 容量=大小; 对于(int i=0;i
将函数定义更改为C++ c+中的模板错误+; #包括 使用名称空间std; 模板 结构术语 { T系数; 国际贸易; }; 模板 类多项式 { 公众: 类术语*术语数组; 国际能力; 国际术语; 多项式(整数大小); ~多项式() { 删除[]TERM数组; } 多项式&Add(多项式b); 多项式&Mul(多项式b); void NewTerm(const float theCoef,const int theExp); void show(); }; 模板 多项式::多项式(int-size)//在没有参数列表的情况下使用模板名“多项式”无效 { 术语=0; 此->termarray=新的T[大小]; 容量=大小; 对于(int i=0;i,c++,templates,polynomial-math,C++,Templates,Polynomial Math,将函数定义更改为 #include<iostream> using namespace std; template<class T> struct term { T coef; int exp; }; template<class T> class Polynomial { public: class term<T> *termarray; int capacity; int t
#include<iostream>
using namespace std;
template<class T>
struct term
{
T coef;
int exp;
};
template<class T>
class Polynomial
{
public:
class term<T> *termarray;
int capacity;
int terms;
Polynomial(int size);
~Polynomial()
{
delete[] termarray;
}
Polynomial& Add(Polynomial b);
Polynomial& Mul(Polynomial b);
void NewTerm(const float theCoef,const int theExp);
void show();
};
template<class T>
Polynomial::Polynomial(int size)// Invalid use of template-name'Polynomial'without argument list
{
terms=0;
this->termarray=new T [size];
capacity=size;
for(int i=0;i<size;i++)
{
cout<<"enter the coefficient of the "<<i<<"th exponent coeffecient"<<endl;
cin>>this->termarray[i].coef;
terms++;
}
}
template<class T>//expected unqualified-id before template
void Polynomial::NewTerm(const float theCoef, const int theExp)
{
if(terms==capacity)
{
capacity=capacity*2;
term *temp=new term[capacity];
copy(termarray,termarray+terms,temp);
delete[]termarray;
termarray=temp;
}
termarray[terms].coef=theCoef;
termarray[terms++].exp=theExp;
}
template<class T>
Polynomial& Polynomial::Add(Polynomial b)//invalid use of template-name 'Polynomial'without an argument list
{
Polynomial c;
int aPos=0,bPos=0;
while((aPos<terms)&&(bPos<b.terms))
{
if(termarray[aPos].exp==b.terarray[bPos].exp){
float t=termarray[aPos].coef+b.termarray[bPos].exp;
if(t) c.NewTerm(t,termarray[aPos].exp);
}
}
else if((termarray[aPos].exp<b.termarray[bPos].exp))
{
c.NewTerm(b.termarray[bPos].coef,b.termarray[bPos].exp);
bPos++;
}
else
{
c.NewTerm(termarray[aPos].coef,termarray[aPos].exp);
aPos++;
}
for(;aPos<terms;Pos++)
{
c.NewTerm(termarray[aPos].coef,termarray[aPos].exp);
}
for(;bPos<b.terms;b++)
c.NewTerm(b.termarray[bPos].coef,b.termarray[bPos].exp);
return c;
}
template<class T>// expected unqualified-id before 'template'
void Polynomial::show()
{
int aPos=0;bPos=0;
for(int i=0;i<terms;i++)
cout<<this->termarray[i].coef<<this->termarray[i].exp;
if(i<terms-1)
{
cout<<"+";
}
}
int main()
{
Polynomial<int> fx(2);
Polynomial<int> fy(2);
Polynomial<int> c(2);
c= fx.Add(fy);
c.show();
return 0;
}
模板多项式::多项式(整数大小){/*impl*/}
等。(注意多项式之后的附加
)。您需要更换
template<class T> Polynomial<T>::Polynomial(int size) { /* impl */ }
借
多项式&Add(多项式b);
多项式&Mul(多项式b);
(顺便问一下,您真的想通过值传递参数a
和b
)。在其他地方,您也可以使用不带模板参数的多项式
。这是不可读的。请正确缩进。感谢您提供完整的程序。它有助于诊断您的问题。但是,您忽略了一个关键元素:问题。您的问题是什么?请使用std::vector
而不是滚动您自己的问题我收到了大量错误(即使在修复了注释行之后)在修改后,决定<代码>术语>代码>是一个类还是一个结构,在<代码>类多项式的声明之后添加一个<代码>;<代码>……这不是真的。C++中有一个概念,叫做注入类名。请查找。函数定义中也需要模板参数,在函数定义中,你不允许为参数编写模板参数。@Alexandre:是的,因为成员函数定义中的参数已经在类中scope@Armen谢谢,我不知道这个班的名字。
Polynomial& Add(Polynomial b);
Polynomial& Mul(Polynomial b);
Polynomial<T>& Add(Polynomial<T> b);
Polynomial<T>& Mul(Polynomial<T> b);