我有下面的代碼來實現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 ==========
你的代碼是否完整?爲什麼時髦的縮進?爲什麼在你的課堂上有兩個'operator +'EMPTY定義?爲什麼EMPTY主要?爲什麼'operator +'的操作數是非const? – paercebal 2010-09-05 14:56:17
你有兩個'op +'的定義?另外,你遇到了什麼樣的錯誤 - 編譯時間,鏈接器或運行時間? – dirkgently 2010-09-05 14:57:43
我會更新代碼外觀 – user439547 2010-09-05 14:58:20