C++ 中的模板错误
Template Error in c++
#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;
}
我在带有注释的行中收到错误(指定错误(。
将函数定义更改为
template<class T> Polynomial<T>::Polynomial(int size) { /* impl */ }
等(请注意Polynomial
后的额外<T>
(。
您需要替换
Polynomial& Add(Polynomial b);
Polynomial& Mul(Polynomial b);
由
Polynomial<T>& Add(Polynomial<T> b);
Polynomial<T>& Mul(Polynomial<T> b);
(顺便说一句,你真的想a
传递你的论点并按值b
吗?在其他地方,您也可以使用没有模板参数的Polynomial
。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'