class template

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear g++ programers:

  I tried to copy a simple code about template and class which use arry
of (int or double or char)
but I can not get pass compile(g++4.5.2, I install gcc 4.5.2 but not
gcc-g++ 4.5.2; however when I g++ -v, it still show it is 4.5.2 from
my old 4.4.3) from a book (C++Primer3rdEd, chapter2).  Please help. Eric
----------------------------------

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
---------------------------------------------------------------




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux