2010-09-05 122 views
0

我有下面的代碼來實現polynimial類實現polynimial類實現

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

的ADT但以下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; 

} 

節點的工作已經參照問題,請告訴我,什麼是錯的? 錯誤

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

你的代碼是否完整?爲什麼時髦的縮進?爲什麼在你的課堂上有兩個'operator +'EMPTY定義?爲什麼EMPTY主要?爲什麼'operator +'的操作數是非const? – paercebal 2010-09-05 14:56:17

+0

你有兩個'op +'的定義?另外,你遇到了什麼樣的錯誤 - 編譯時間,鏈接器或運行時間? – dirkgently 2010-09-05 14:57:43

+0

我會更新代碼外觀 – user439547 2010-09-05 14:58:20

回答

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+
+0

不,如果它幫助我使用visual C++ 2010並且錯誤是相同的,那麼它是不固定的 – user439547 2010-09-05 15:13:16

+0

我發佈的代碼在VS2010和Comeau上編譯得很好。你可以嘗試將它複製粘貼到編輯器窗口並構建它嗎? – dirkgently 2010-09-05 15:19:29

2

你在你的代碼肯定有太多的operator+() defintions:

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 { 
    [...] 
} 
0

看來你錯過了一個重載operator<<爲你的聚類。你看,cout << y只是不奇蹟般地工作。你必須確定這裏應該發生什麼。

一對夫婦的其他建議:

  • 使用const時,它是有道理的。例如,它對於operator+,operator*operator<<作爲您的參考參數的cv限定符很有意義。
  • 由於您似乎在學習語言,因此瞭解如何編寫複製構造函數,賦值運算符和析構函數(尊重rule of three)可能非常有益。但在實際的代碼中,你可能只是使用一個向量作爲成員,使得生活如此更容易,因爲你幾乎不需要做任何事情(w.r.t.定義你自己的特殊成員函數)。

例子:

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

有!不需要自定義副本,賦值運算符或析構函數。