多项式类实现的实现

时间:2010-09-05 14:51:51

标签: c++

我有以下代码来实现多项式类的ADT

#include <iostream>
#include <cstdlib>
using namespace std;
template<class Number>
class POLY
{
public:
    int n;
     Number *a;
public:
    POLY<Number>(Number c,int N){

        a=new Number[N+1];
          n=N+1;
            a[N]=c;
             for (int i=0;i<N;i++) a[i]=0;
                }
    float eval( float x)  const {

         double t=0.0;
          for (int i=n-1;i>=0;i--)
               t=t*x+a[i];
           return t;


    }


     friend POLY operator+(POLY &p,POLY&q){
        friend POLY operator+(POLY &p,POLY&q){

POLY t(0,p.n>q.n?p.n-1:q.n-1);
           for (int i=0;i<p.n;i++)
 t.a[i]+=p.a[i];
 for (int j=0;j<q.n;j++)
   t.a[j]+=q.a[j];

   return t;

}



     }
     friend POLY operator+(POLY &p,POLY&q){



     }

}


int main(){






     return 0;
}

但节点在分段后工作

friend POLY operator+(POLY &p,POLY&q){

 POLY t(0,p.n>q.n?p.n-1:q.n-1);
           for (int i=0;i<p.n;i++)
 t.a[i]+=p.a[i];
 for (int j=0;j<q.n;j++)
   t.a[j]+=q.a[j];

   return t;

}

它有参考问题请告诉我有什么问题? 错误

1>------ Build started: Project: polynomial, Configuration: Debug Win32 ------
1>Build started 9/5/2010 6:00:34 PM.
1>PrepareForBuild:
1>  Creating directory "c:\users\david\documents\visual studio 2010\Projects\polynomial\Debug\".
1>InitializeBuildStatus:
1>  Creating "Debug\polynomial.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  polynomial.cpp
1>c:\users\david\documents\visual studio 2010\projects\polynomial\polynomial\polynomial.cpp(54): error C2143: syntax error : missing ';' before 'int'
1>c:\users\david\documents\visual studio 2010\projects\polynomial\polynomial\polynomial.cpp(29): error C2270: '+' : modifiers not allowed on nonmember functions
1>          c:\users\david\documents\visual studio 2010\projects\polynomial\polynomial\polynomial.cpp(54) : see reference to class template instantiation 'POLY<Number>' being compiled
1>c:\users\david\documents\visual studio 2010\projects\polynomial\polynomial\polynomial.cpp(45): error C2270: '+' : modifiers not allowed on nonmember functions
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.36
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我已更新代码

#include <iostream>
#include <cstdlib>
#include <ostream>
using namespace std;

template<class Number>
class POLY
{
public:
    int n;
    Number *a;
public:
    POLY(Number c,int N){
        a=new Number[N+1];
        n=N+1;
        a[N]=c;
        for (int i=0;i<N;i++) a[i]=0;
    }

    float eval( float x)  const {
        double t=0.0;
        for (int i=n-1;i>=0;i--)
            t=t*x+a[i];
        return t;
    }

    friend POLY operator+(POLY &p,POLY&q){

        POLY t(0,p.n>q.n?p.n-1:q.n-1);
        for (int i=0;i<p.n;i++)
           t.a[i]+=p.a[i];
        for (int j=0;j<q.n;j++)
           t.a[j]+=q.a[j];
        return t;
    }
     friend POLY operator*(POLY &p,POLY & q){

         POLY t(0,(p.n-1)+(q.n-1));
              for (int i=0;i<p.n;i++)
                     for (int j=0;j<q.n;j++)
                           t.a[i+j]=p.a[i] * q.a[j];
                return  t;

     }
};


int main(int argc,char *argv[]){
    float p=3.0f;
    int n=10;

    POLY<int> x(1, 1),one(1,0),t=x+one,y=t;
     for (int i=0;i<n;i++)
     {  y=y*t;
     cout<<y<<endl;}
     cout<<y.eval(p)<<endl;

    return 0;
}

错误

>c:\users\david\documents\visual studio 2010\projects\polynomial1\polynomial1\polynomial.cpp(55): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'POLY<Number>' (or there is no acceptable conversion)
1>          with
1>          [
1>              Number=int
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(726): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(764): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(811): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(937): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(944): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(951): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(958): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(968): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>,POLY<Number>>(std::basic_ostream<_Elem,_Traits> &&,_Ty)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              Number=int,
1>              _Ty=POLY<int>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(1085): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(186): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(192): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(199): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(206): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(226): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(260): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(280): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(305): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(325): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(345): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(366): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(386): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(407): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(427): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(447): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(467): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(487): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::ostream, POLY<Number>)'
1>          with
1>          [
1>              Number=int
1>          ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.82
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

3 个答案:

答案 0 :(得分:2)

classstruct声明以';'结尾。

#include <iostream>
#include <cstdlib>

using namespace std;

template<class Number>
class POLY
{
public:
    int n;
    Number *a;
public:
    POLY(Number c,int N){
        a=new Number[N+1];
        n=N+1;
        a[N]=c;
        for (int i=0;i<N;i++) a[i]=0;
    }

    float eval( float x)  const {
        double t=0.0;
        for (int i=n-1;i>=0;i--)
            t=t*x+a[i];
        return t;
    }

    friend POLY operator+(POLY &p,POLY&q){

        POLY t(0,p.n>q.n?p.n-1:q.n-1);
        for (int i=0;i<p.n;i++)
           t.a[i]+=p.a[i];
        for (int j=0;j<q.n;j++)
           t.a[j]+=q.a[j];
        return t;
    }
};


int main(){
    POLY<int> p(5, 2);
    return 0;
}

以上是应该编译的代码的固定版本。您的设计可以使用一些改进。一些建议:

  • 了解三巨头的规则(复制构造函数,复制赋值运算符和析构函数)
  • 不要从类模板开始(如果你刚开始用C ++编程)
  • 初始化列表
  • 在测试程序中using namespace std;可以,但你必须知道命名空间污染
  • 如果您执行new,则需要delete某处(而new ...[]需要delete [] ...),否则您会泄漏内存
  • 您的代码不会使用iostreamcstdlib中的任何内容。为什么要保留它们呢?
  • for
  • 中不需要两个friend op+循环

答案 1 :(得分:2)

您的代码中肯定有太多operator+()次定义:

 friend POLY operator+(POLY &p,POLY&q){
    friend POLY operator+(POLY &p,POLY&q){
        [...]
    }
 }
 friend POLY operator+(POLY &p,POLY&q){

 }

显然应该只有其中一个。

您提到的“引用问题”可能意味着您应该通过const引用获取参数(并且运算符本身也应该是const):

POLY operator+(const POLY &p, const POLY &q) const {
    [...]
}

答案 2 :(得分:0)

您的多边形类似乎缺少重载operator<<。你看,cout << y只是不神奇地工作。你必须在这里确定应该发生什么。

其他一些建议:

  • 只要有意义,请使用const。例如,operator+operator*operator<<作为参考参数的cv限定符很有意义。
  • 由于您似乎正在学习该语言,因此了解如何编写复制构造函数,赋值运算符和析构函数(尊重rule of three)可能非常有益。但是在实际的代码中,你可能只是使用一个向量作为成员,这使得生活所以更容易,因为你几乎不需要做任何事情(定义你自己的特殊成员函数)。

示例:

template<class T>
struct poly
{
    std::vector<T> coeffs;

    // default ctor
    poly() {} 

    // conversion constructor (i.e. poly<int> --> poly<double>
    template<class U>
    poly(poly<U> const& p)
    : coeffs(p.coeffs.begin(),p.coeffs.end()) {}

    friend poly operator+(poly const& a, poly const& b)
    {
      ...
    }

    friend poly operator*(poly const& a, poly const& b)
    {
      ...
    }
};

template<class T>
ostream& operator<<(ostream& os, poly<T> const& p)
{
    ....
    return os;
}

有!无需自定义复制ctor,赋值运算符或析构函数。