c++ 中的模板错误

发布于 2024-12-04 12:06:36 字数 2608 浏览 0 评论 0原文

#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;
    }

我在评论中收到错误(指定错误)。

#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;
    }

i am getting errors in lines with comments(specified the error).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-12-11 12:06:36

将函数定义更改为

template<class T> Polynomial<T>::Polynomial(int size) { /* impl */ }

等(请注意 Polynomial 之后的附加 )。

Change your function definitions to

template<class T> Polynomial<T>::Polynomial(int size) { /* impl */ }

etc. (note the additional <T> after Polynomial).

成熟稳重的好男人 2024-12-11 12:06:36

您需要替换

Polynomial& Add(Polynomial b);
Polynomial& Mul(Polynomial b);

Polynomial<T>& Add(Polynomial<T> b);
Polynomial<T>& Mul(Polynomial<T> b);

(顺便说一句,您真的想按值传递参数 ab 吗?)。还有其他地方可以使用不带模板参数的多项式。

You need to replace

Polynomial& Add(Polynomial b);
Polynomial& Mul(Polynomial b);

by

Polynomial<T>& Add(Polynomial<T> b);
Polynomial<T>& Mul(Polynomial<T> b);

(BTW, do you really want to pass your arguments a and b by value?). There are other places too where you use Polynomial without template arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文