[C++] Linkage Error w/ Code Sample from Lippman...

From: entropy123 (email_entropy123_at_yahoo.com)
Date: 02/25/04


Date: 25 Feb 2004 13:02:31 -0800

Hey all,

This is my first go at derived/base classes and I'm having some
trouble...

I'm getting the following error in Visual Studio 2003:

TourC++ error LNK2019: unresolved external symbol "public: __thiscall
IntArrayRC::IntArrayRC(int *,int)" (??0IntArrayRC@@QAE@PAHH@Z)
referenced in function _main

Lippman's code, copied into visual studio by me, is below: (PS: This
is my transcription of the 3rd edition code, he has some samples on
the web, but they are very different...and are chock full of
preprocessor commands...which I don't know yet either...)

#include "main.h"

int main() {

        int array[4] = {0,1,2,3};
        IntArray ia1( array, 4 );
        IntArrayRC ia2( array, 4 );

        cout << "swap() with IntArray ia1" << endl;
        swap( ia1, 1, ia1.size() );

        cout << "swap() with IntArrayRC ia2" << endl;
        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 MAIN_H
#define MAIN_H

#include <iostream>
#include <cassert>
using namespace std;

#include "array.h"
#include "rcarray.h"

void swap( IntArray &ia, int i, int j );

#endif

#include "main.h"

int&
IntArray::
operator[]( int index )
{
        assert( index >= 0 && index < size() );
        return ia[ index ];

}

void
IntArray::
init( int sz, int *array )
{
        _size = sz;
        ia = new int[ _size ];

        for ( int ix = 0; ix < _size; ++ix ) {
                if ( !array ) {
                        ia[ ix ] = 0;
                } else {
                        ia[ ix ] = array[ ix ];
                }
        }
} //IntArray::init

#ifndef ARRAY_H
#define ARRAY_H

class IntArray {

public:
        explicit IntArray( int sz = DefaultArraySize ) { init( sz, 0); };
        IntArray( int *array, int array_size ) { init(array_size, array); };
        IntArray( const IntArray &rhs ) { init( rhs.size(), rhs.ia); } ;

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

        bool operator==( const IntArray& ) const;
        bool operator!=( const IntArray& ) const;

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

        virtual int& operator[]( int index );
        //virtual void sort();

        //virtual int min() const;
        //virtual int max() const;
        //virtual int find( int value ) const;

protected:
        static const int DefaultArraySize = 12;
        void init( int sz, int *array );

        int _size;
        int *ia;
};

#endif

#include "main.h"

inline int&
IntArrayRC::operator[]( int index )
{
        check_range( index );
        return ia[ index ];

}

inline void
IntArrayRC::check_range( int index )
{
        assert( index >= 0 && index < size() );
}

inline IntArrayRC::IntArrayRC(int sz): IntArray( sz ){}

inline IntArrayRC::IntArrayRC( int *iar, int sz ): IntArray( iar, sz )
{}

#ifndef RCARRAY_H
#define RCARRAY_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



Relevant Pages