c++primer P40的例子为什么编译通不过?

beelin 2003-09-28 12:47:45
-------------Array.h-------------
template <class elemType>
class Array{
public:
explicit Array(int size=DefaultArraySize);
Array(elemType *array,int array_size);
Array(const Array &rhs);

virtual ~Array(){ delete [] ia;}

bool operator==(const Array&)const;
bool operator!=(const Array&)const;
Array& operator=(const Array&);
int size() const {return _size;}

virtual elemType& operator[](int index){return ia[index];}
virtual void sort();

virtual elemType min() const;
virtual elemType max() const;
virtual int find(const elemType &value)const;

protected:
static const int DefaultArraySize = 12;

int _size;
elemType *ia;
};
----------------4.cpp-----------------
#include <iostream>
#include "Array.h"
using namespace std;

int main()
{
const int array_size=4;
Array<int> ia(array_size);
Array<double> da(array_size);
Array<char> ca(array_size);

int ix;
for (ix=0;ix<array_size;++ix)
{
ia[ix]=ix;
da[ix]=ix*1.75;
ca[ix]=ix+'a';
}

for (ix=0;ix<array_size;++ix)
cout<<"[ "<<ix<<" ] ia: "<<ia[ix]
<<"\tca: "<<ca[ix]
<<"\tda: "<<da[ix]<<endl;
return 0;
}
VC6下编译时出现错误:
--------------------Configuration: 4 - Win32 Debug--------------------
Compiling...
4.cpp
e:\vc程序\c++\4\array.h(24) : error C2258: illegal pure syntax, must be '= 0'
e:\vc程序\c++\4\array.h(28) : see reference to class template instantiation 'Array<elemType>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2252: 'DefaultArraySize' : pure specifier can only be specified for functions
e:\vc程序\c++\4\array.h(28) : see reference to class template instantiation 'Array<elemType>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2258: illegal pure syntax, must be '= 0'
e:\vc程序\c++\4\4.cpp(8) : see reference to class template instantiation 'Array<int>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2252: 'DefaultArraySize' : pure specifier can only be specified for functions
e:\vc程序\c++\4\4.cpp(8) : see reference to class template instantiation 'Array<int>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2258: illegal pure syntax, must be '= 0'
e:\vc程序\c++\4\4.cpp(9) : see reference to class template instantiation 'Array<double>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2252: 'DefaultArraySize' : pure specifier can only be specified for functions
e:\vc程序\c++\4\4.cpp(9) : see reference to class template instantiation 'Array<double>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2258: illegal pure syntax, must be '= 0'
e:\vc程序\c++\4\4.cpp(10) : see reference to class template instantiation 'Array<char>' being compiled
e:\vc程序\c++\4\array.h(24) : error C2252: 'DefaultArraySize' : pure specifier can only be specified for functions
e:\vc程序\c++\4\4.cpp(10) : see reference to class template instantiation 'Array<char>' being compiled
Error executing cl.exe.

4.exe - 8 error(s), 0 warning(s)

请指点,谢谢!
...全文
153 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ThinkX 2003-09-28
  • 打赏
  • 举报
回复
不要用VC6这么古老的编译器,用VC7.1,BCB6,MinGW 3.1
beelin 2003-09-28
  • 打赏
  • 举报
回复
tolixiaohui,先谢谢你,你的程序完善了些,但好像不能解决问题。

static const int DefaultArraySize = 12;
这样定义应该没问题啊,我改为0,还是有问题。
mouseleeky 2003-09-28
  • 打赏
  • 举报
回复
是编译器的问题。
VC6与标准C++的兼容还不是很好。
以后会有很多例子说明这点(在C++Primer中)。
beelin 2003-09-28
  • 打赏
  • 举报
回复
VC6虽老了些,但与C++标准是兼容的啊,是编译器的问题?
tolixiaohui 2003-09-28
  • 打赏
  • 举报
回复
//Array.cpp
#ifndef ARRAY_C
#define ARRAY_C

#include <assert.h>
#include "Array.h"

template <class elemType>
const int Array<elemType>::DefaultArraySize = 12;

template <class elemType>
void Array<elemType>::init( const elemType *array, int sz )
{
if ( ! array ) { _size = 0; _ia = 0; }
if ( sz < 1 ) sz = 1;

_size = sz;
_ia = new elemType[ _size ];

if ( ! array ) return;

for ( int ix = 0; ix < _size; ++ix )
_ia[ ix ] = array[ ix ];
}

template <class elemType> Array<elemType>&
Array<elemType>::operator=( const Array<elemType> &iA )
{
if ( this == &iA )
return *this;

delete[] _ia;
init( iA._ia, iA._size );
return *this;
}

template <class elemType> ostream&
operator<<( ostream &os, Array<elemType> &ar )
{
return ar.print( os );
}

template <class elemType>
ostream& Array<elemType>::print( ostream &os ) const
{
const int lineLength = 12;
os << "( " << _size << " )< ";

for ( int ix = 0; ix < _size; ++ix )
{
if ( ix % lineLength == 0 && ix )
os << "\n\t";

os << _ia[ ix ];
// don't generate comma for last item on line
// nor for the last element of the array
if ( ix % lineLength != lineLength-1 && ix != _size-1 )
os << ", ";
}

os << " >\n";
return os;
}

template <class elemType>
void Array<elemType>::grow()
{
elemType *oldia = _ia;
int oldSize = _size;

_size = oldSize + oldSize/2 + 1;
_ia = new elemType[_size];

int ix;
for ( ix = 0; ix < oldSize; ++ix)
_ia[ix] = oldia[ix];

for ( ; ix < _size; ++ix )
_ia[ix] = elemType();

delete[] oldia;
}

template <class elemType>
elemType Array<elemType>::min( )
{
assert( _ia != 0 );
elemType min_val = _ia[0];

for ( int ix = 1; ix < _size; ++ix )
if ( _ia[ix] < min_val )
min_val = _ia[ix];

return min_val;
}

template <class elemType>
elemType Array<elemType>::max()
{
assert( _ia != 0 );
elemType max_val = _ia[0];

for ( int ix = 1; ix < _size; ++ix )
if ( max_val < _ia[ix] )
max_val = _ia[ix];

return max_val;
}

template <class elemType>
int Array<elemType>::find( const elemType &val )
{
for ( int ix = 0; ix < _size; ++ix )
if ( val == _ia[ix] )
return ix;

return -1;
}

template <class elemType>
void Array<elemType>::swap( int i, int j )
{
elemType tmp = _ia[i];
_ia[i] = _ia[j];
_ia[j] = tmp;
}

template <class elemType>
void Array<elemType>::sort( int low, int high )
{
if ( low >= high ) return;
int lo = low;
int hi = high + 1;
elemType elem = _ia[low];

for ( ;; ) {
while ( _ia[++lo] < elem ) ;
while ( _ia[--hi] > elem ) ;
if ( lo < hi )
swap( lo,hi );
else break;
}

swap( low, hi );
sort( low, hi-1 );
sort( hi+1, high );
}

#endif
tolixiaohui 2003-09-28
  • 打赏
  • 举报
回复
//Array.h
#ifndef ARRAY_H
#define ARRAY_H


#include <iostream.h>

template <class elemType> class Array;
template <class elemType> ostream&
operator<<( ostream &, Array<elemType> & );

template <class elemType>
class Array {
public:
// explicit Array( int sz = DefaultArraySize )
Array( int sz = DefaultArraySize )
{ init( 0, sz ); }

Array( const elemType *ar, int sz )
{ init( ar, sz ); }

Array( const Array &iA )
{ init( iA._ia, iA._size ); }

virtual ~Array() { delete[] _ia; }

Array& operator=( const Array & );
int size() const { return _size; }

virtual elemType& operator[]( int ix ) { return _ia[ix]; }
virtual elemType operator[]( int ix ) const { return _ia[ix]; }

elemType at( int ix ) const { return _ia[ ix ]; }

virtual ostream &print( ostream& os = cout ) const;
virtual void grow();
virtual void sort( int,int );
virtual int find( const elemType& );

virtual elemType min();
virtual elemType max();

protected:
void init( const elemType*, int );
void swap( int, int );

// static const int DefaultArraySize = 12;
static const int DefaultArraySize;
int _size;
elemType *_ia;
};

#endif

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧