Dear Mr. Bjarne Stroustrup: Thanks your suggestion, I follow it but it show fatal error:Array.cpp: no such file so I add #include "Array.cpp" in my main program, pg52.cpp then it can compile but when I run it, it response Segmentation fault
again it's g++ 4.5.2. What may cause wrong? hope to see your suggestion/advice again, and thanks a lot in advance, Eric ----------------------------------------------------- On Fri, 2011-06-10 at 22:18 -0400, bs wrote: > On 6/10/2011 9:43 PM, eric wrote: > > Dear Bjarne Stroustrup: > > > > I copied a piece of codes from book(C++ primer 3rd Ed) chapter 2 about > > page 52. > > but it failed to be compiled under my g++ 4.5.2 > > here is the program > > That book is an antique, but I think that the problem may simply be that > you can't separately compile pg52.cpp and Array.cpp. Try > #include<Array.cpp> in Array.h . > > > ----------------------- > > > > eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp > > /tmp/ccE9MPMg.o: In function `main': > > pg52.cpp:(.text+0x23): undefined reference to `Array<int>::Array(int)' > > pg52.cpp:(.text+0x37): undefined reference to > > `Array<double>::Array(int)' > > pg52.cpp:(.text+0x4b): undefined reference to `Array<char>::Array(int)' > > collect2: ld returned 1 exit status > > -------------------------------------------------- > > #include<iostream> > > #include "Array.h" > > > > > > > > int main() { > > const int array_size = 4; > > > > // elemType becomes int > > Array<int> ia(array_size); > > > > // elemType becomes double > > Array<double> da(array_size); > > > > // elemType becomes char > > 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 ) > > std::cout<< "[ "<< ix<< " ] ia: "<< ia[ix] > > << "\tca: "<< ca[ix] > > << "\tda: "<< da[ix]<< std::endl; > > > > return 0; > > } > > ------------------- > > // Array.cpp > > > > #include "Array.h" > > > > /* > > Array::Array(elemType *iarray, int iarray_size) > > { > > ia = iarray; > > _size = iarray_size; > > } > > */ > > > > template<class T> Array<T>::Array(const Array<T> &rhs ) > > { > > ia = rhs; > > } > > > > template<class T> Array<T>::Array(const int size ) > > { > > _size = size; > > } > > > > > > ------------------------------------------------------ > > #ifndef Array_H > > #define Array_H > > > > > > template< class elemType> > > class Array { > > public: > > // parameterize element type > > 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; > > }; > > > > #endif > > --------------------------------------------------------------- > > I asked mailing list in gcc, but so far I did not get any useful > > response. I know the book's author Stanley B. Lippman or Josee Lajoie > > ever work with you. And I tried to reach Stanley B. Lippman, I can not > > find his email addreess. I emailed Josee Lajoie but again never get any > > message from her (for 2 or 3 days). > > hope to get your help on this problem. > > Thanks your efforts a lot in advance > > Eric > > > > >