undefined reference(compile error), plz help
Dear advance c++ programers:
I tried to type in/test your code in chapter 2 of c++ primer (3rd
ed)
page 45, 46, 49
IntArray.h
IntArrayRC.h
(and add IntArray.cpp myself) /* they are all simple */
the following is my code copied from your book
----------------------------------------------
#include <iostream>
#include "IntArray.h"
#include "IntArrayRC.h"
using namespace std;
void swap(IntArray&, int, int);
int main() {
int array[ 4 ] = { 0, 1, 2, 3 };
IntArray ia1( array, 4 );
IntArrayRC ia2( array, 4 );
// error: off-by-one: should be size - 1
// not caught by IntArray object
cout << "swap() with IntArray ia1\n";
swap( ia1, 1, ia1.size() );
// ok: caught by IntArrayRC object
cout << "swap() with IntArrayRC ia2\n";
swap( ia2, 1, ia2.size() );
return 0;
}
void swap( IntArray &ia, int i, int j )
{
int tmp = ia[ i ];
ia[ i ] = ia[ j ];
ia[ j ] = tmp;
}
----------------------------------------------------
#ifndef IntArray_H
#define IntArray_H
class IntArray {
public:
// equality and inequality operations: #2b
explicit IntArray( int size = DefaultArraySize );
IntArray( int *array, int array_size );
IntArray( const IntArray &rhs );
// virtual destructor!
virtual ~IntArray() { delete [] ia; }
// equality and inequality operations:
bool operator==( const IntArray& ) const;
bool operator!=( const IntArray& ) const;
// assignment operator: #2a
IntArray& operator=( const IntArray& );
int size() const { return _size; }
// we've removed the check on the index...
virtual int& operator[](int index) { return ia[index]; }
// int size() const; // #1
virtual void sort(); // #4
virtual int min() const; // #3a
virtual int max() const; // #3b
// if the value is found within the aray,
// return the index of its first occurrence
// otherwise, return -1
virtual int find ( int value ) const; // #3c
protected:
// the private implementation
static const int DefaultArraySize = 12;
void init( int sz, int *array );
int _size;
int *ia;
};
#endif
----------------------------------------------
#ifndef IntArrayRC_H
#define IntArrayRC_H
#include "IntArray.h"
class IntArrayRC : public IntArray {
public:
IntArrayRC( int sz = DefaultArraySize );
IntArrayRC( int *array, int array_size );
IntArrayRC( const IntArrayRC &rhs );
virtual int& operator[]( int );
private:
void check_range( int );
};
#endif
-------------------------------------------------
// IntArray.cpp
#include "IntArray.h"
IntArray::IntArray(int *iarray, int iarray_size)
{
ia = iarray;
_size = iarray_size;
}
------------------------------------------------------------
but my compile result( errors) is
--
eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp pg49.cpp
/tmp/ccOnEojJ.o: In function `IntArray::IntArray(int*, int)':
IntArray.cpp:(.text+0x8): undefined reference to `vtable for IntArray'
/tmp/ccdxV8oM.o: In function `main':
pg49.cpp:(.text+0x5e): undefined reference to
`IntArrayRC::IntArrayRC(int*, int)'
/tmp/ccdxV8oM.o: In function `IntArray::~IntArray()':
pg49.cpp:(.text._ZN8IntArrayD2Ev[_ZN8IntArrayD5Ev]+0xb): undefined
reference to `vtable for IntArray'
/tmp/ccdxV8oM.o: In function `IntArrayRC::~IntArrayRC()':
pg49.cpp:(.text._ZN10IntArrayRCD2Ev[_ZN10IntArrayRCD5Ev]+0xb):
undefined reference to `vtable for IntArrayRC'
collect2: ld returned 1 exit status
eric@eric-laptop:~/CppPrimer3$
---------
you can see, in my IntArray.cpp, I already defined IntArray
constructor, but why I still got
In function IntArray::IntArray(int*, int) undefined reference to
'vtable for IntArray'
plz help, thank a lot in advance, Eric