FWIW Array( const Array&rhs );
On Fri, Jun 10, 2011 at 08:30:05PM -0700, eric wrote: > 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 > > > > > > > > > -- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 "Yeah - I write Free Software...so SUE ME" "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society." "> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one." © Copyright for the Digital Millennium