我有以下代码来实现多项式类的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 ==========
答案 0 :(得分:2)
class
和struct
声明以';'结尾。
#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;
}
以上是应该编译的代码的固定版本。您的设计可以使用一些改进。一些建议:
using namespace std;
可以,但你必须知道命名空间污染new
,则需要delete
某处(而new ...[]
需要delete [] ...
),否则您会泄漏内存iostream
或cstdlib
中的任何内容。为什么要保留它们呢?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限定符很有意义。示例:
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,赋值运算符或析构函数。