开发者

implementation of polynimial class implementation

i have following code to implement ADT of polynimial class

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

but does node work following pragment

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;

}

it has problem with reference please tell me what is wrong? errors

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 ==========

i have updated code

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

errors are

>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>
开发者_如何学Python1>          ]
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 ==========


class and struct declarations end with a ';'.

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

The above is a fixed version of your code that should compile. Your design could use some improvement though. Some suggestions:

  • Learn about the rule of Big Three(copy constructor, copy assignment operator and destructor)
  • Do not start off with a class template (if you are only beginning programming in C++)
  • Initializer lists
  • It's okay to using namespace std; in a test program but you must know about namespace pollution
  • If you do a new you need a delete somewhere (and a new ...[] requires a delete [] ...) or else you leak memory
  • Your code doesn't use anything either from iostream or cstdlib. Why keep them then?
  • You don't need two for loops in your friend op+


You have definitely too many operator+() defintions in your code:

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

 }

Obviously there should only be one of them.

The "problem with references" you mention probably means that you should take the parameters by const reference (and the operator itself should also be const):

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


It seems you're missing an overloaded operator<< for your poly class. You see, cout << y just doesn't magically work. You have to define what exactly should happen here.

A couple of other suggestions:

  • Use const whenever it makes sense. For example, it makes a lot of sense for operator+, operator* and operator<< as a cv qualifier for your reference arguments.
  • Since you seem to be learning the language it might be very instructive to learn about how to write a copy constructor, an assignment operator and a destructor (respecting the rule of three). But in actual code, you would probably just use a vector as member which makes life so much easier as you practically don't have to do anything (w.r.t. defining your own special member functions).

Example:

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

There! No need for custom copy ctor, assignment operator or destructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜