undefined reference(compile error), plz help

Discussion in 'C++' started by eric, Jun 9, 2011.

  1. eric

    eric Guest

    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
    eric, Jun 9, 2011
    #1
    1. Advertising

  2. eric

    eric Guest

    On Jun 9, 12:44 am, Paavo Helde <> wrote:
    > eric <> wrote in news:33ca3f4e-bbef-4072-9fc6-
    > :
    >
    > > 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

    >
    > [...]
    >
    > > 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'

    >
    > With g++ this typically means you have not provided definition of the
    > first non-inline virtual function in this class. As far as I can see,
    > this would be IntArray::Sort(). Provide a definition of this function
    > (and all other non-defined virtual functions) in the relevant .cpp file.
    >
    > In C++ one can in principle leave out definitions for functions which are
    > not used, but all virtual functions are effectively "used" for populating
    > the vtable entries, so they must be all present in the program.
    >
    > hth
    > Paavo


    /
    ***********************************************************************************************/

    Thanks Paavo and Brian your reply on this undefined reference

    now I delete more variables, functions in my IntArray.h and add
    IntArrayRC.cpp(and IntArray.cpp)
    /
    ***********************************************************************************************/
    #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 _size = DefaultArraySize );
    IntArrayRC( int *array, int array_size );
    IntArrayRC( const IntArrayRC &rhs );
    /* virtual*/ int& operator[]( int );

    private:
    void check_range( int );
    };

    #endif
    /*----------------------------*/
    // IntArrayRC.cpp

    #include "IntArrayRC.h"

    IntArrayRC::IntArrayRC(int *iarray, int iarray_size)
    {
    ia = iarray;
    _size = iarray_size;
    }
    /*-------------------------------------------------------*/
    // IntArray.cpp

    #include "IntArray.h"

    IntArray::IntArray(int *iarray, int iarray_size)
    {
    ia = iarray;
    _size = iarray_size;
    }

    /*-------------------------------------------------------*/
    /* my pg49.cpp and IntArray.cpp didn't change */
    /*--------- ----------*/
    /* I tried to make everything simple, just hope to make it run, but
    it still not work, and I don't see my IntArrayRC.cpp or pg49.cpp
    reference
    * IntArray::IntArray(int)' at all. how come compiler complain
    that?
    */
    eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp IntArrayRC.cpp
    pg49.cpp
    /tmp/ccFlMagZ.o: In function `IntArrayRC::IntArrayRC(int*, int)':
    IntArrayRC.cpp:(.text+0x15): undefined reference to
    `IntArray::IntArray(int)'
    collect2: ld returned 1 exit status
    eric@eric-laptop:~/CppPrimer3$


    looking to see any advancer's help again, and
    Thanks a lot in advance,
    Eric
    eric, Jun 9, 2011
    #2
    1. Advertising

  3. On 6/9/2011 1:25 PM, eric wrote:
    > On Jun 9, 12:44 am, Paavo Helde<> wrote:
    >> eric<> wrote in news:33ca3f4e-bbef-4072-9fc6-
    >> :
    >>
    >>> 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

    >>
    >> [...]
    >>
    >>> 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'

    >>
    >> With g++ this typically means you have not provided definition of the
    >> first non-inline virtual function in this class. As far as I can see,
    >> this would be IntArray::Sort(). Provide a definition of this function
    >> (and all other non-defined virtual functions) in the relevant .cpp file.
    >>
    >> In C++ one can in principle leave out definitions for functions which are
    >> not used, but all virtual functions are effectively "used" for populating
    >> the vtable entries, so they must be all present in the program.
    >>
    >> hth
    >> Paavo

    >
    > /
    > ***********************************************************************************************/
    >
    > Thanks Paavo and Brian your reply on this undefined reference
    >
    > now I delete more variables, functions in my IntArray.h and add
    > IntArrayRC.cpp(and IntArray.cpp)
    > /
    > ***********************************************************************************************/
    > #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 _size = DefaultArraySize );
    > IntArrayRC( int *array, int array_size );
    > IntArrayRC( const IntArrayRC&rhs );
    > /* virtual*/ int& operator[]( int );
    >
    > private:
    > void check_range( int );
    > };
    >
    > #endif
    > /*----------------------------*/
    > // IntArrayRC.cpp
    >
    > #include "IntArrayRC.h"
    >
    > IntArrayRC::IntArrayRC(int *iarray, int iarray_size)
    > {
    > ia = iarray;
    > _size = iarray_size;
    > }
    > /*-------------------------------------------------------*/
    > // IntArray.cpp
    >
    > #include "IntArray.h"
    >
    > IntArray::IntArray(int *iarray, int iarray_size)
    > {
    > ia = iarray;
    > _size = iarray_size;
    > }
    >
    > /*-------------------------------------------------------*/
    > /* my pg49.cpp and IntArray.cpp didn't change */
    > /*--------- ----------*/
    > /* I tried to make everything simple, just hope to make it run, but
    > it still not work, and I don't see my IntArrayRC.cpp or pg49.cpp
    > reference
    > * IntArray::IntArray(int)' at all. how come compiler complain
    > that?
    > */
    > eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp IntArrayRC.cpp
    > pg49.cpp
    > /tmp/ccFlMagZ.o: In function `IntArrayRC::IntArrayRC(int*, int)':
    > IntArrayRC.cpp:(.text+0x15): undefined reference to
    > `IntArray::IntArray(int)'
    > collect2: ld returned 1 exit status
    > eric@eric-laptop:~/CppPrimer3$
    >
    >
    > looking to see any advancer's help again, and
    > Thanks a lot in advance,


    You declared the constructor in 'IntArray' that takes a single 'int'
    argument, but the argument has a default value, right? So, that makes
    it the default constructor. Since 'IntArrayRC' does not initialize its
    base class in any way, the compiler must use the default constructor.
    And you didn't define it anywhere. Define it, and make sure your
    compiler knows where it is.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 9, 2011
    #3
  4. eric

    eric Guest

    On Jun 9, 10:44 am, Victor Bazarov <> wrote:
    > On 6/9/2011 1:25 PM, eric wrote:
    >
    >
    >
    > > On Jun 9, 12:44 am, Paavo Helde<>  wrote:
    > >> eric<>  wrote in news:33ca3f4e-bbef-4072-9fc6-
    > >> :

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

    >
    > >> [...]

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

    >
    > >> With g++ this typically means you have not provided definition of the
    > >> first non-inline virtual function in this class. As far as I can see,
    > >> this would be IntArray::Sort(). Provide a definition of this function
    > >> (and all other non-defined virtual functions) in the relevant .cpp file.

    >
    > >> In C++ one can in principle leave out definitions for functions which are
    > >> not used, but all virtual functions are effectively "used" for populating
    > >> the vtable entries, so they must be all present in the program.

    >
    > >> hth
    > >> Paavo

    >
    > > /
    > > ***********************************************************************************************/

    >
    > > Thanks Paavo and Brian your reply on this undefined reference

    >
    > > now I delete more variables, functions in my IntArray.h and add
    > > IntArrayRC.cpp(and IntArray.cpp)
    > > /
    > > ***********************************************************************************************/
    > > #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 _size = DefaultArraySize );
    > >    IntArrayRC( int *array, int array_size );
    > >    IntArrayRC( const IntArrayRC&rhs );
    > >   /* virtual*/ int&  operator[]( int );

    >
    > > private:
    > >    void check_range( int );
    > > };

    >
    > > #endif
    > > /*----------------------------*/
    > > // IntArrayRC.cpp

    >
    > > #include "IntArrayRC.h"

    >
    > > IntArrayRC::IntArrayRC(int *iarray, int iarray_size)
    > > {
    > >    ia = iarray;
    > >    _size = iarray_size;
    > > }
    > > /*-------------------------------------------------------*/
    > > // IntArray.cpp

    >
    > > #include "IntArray.h"

    >
    > > IntArray::IntArray(int *iarray, int iarray_size)
    > > {
    > >    ia = iarray;
    > >    _size = iarray_size;
    > > }

    >
    > > /*-------------------------------------------------------*/
    > > /* my pg49.cpp and IntArray.cpp didn't change            */
    > > /*---------                                   ----------*/
    > > /*  I tried to make everything simple, just hope to make it run, but
    > > it still not work, and I don't see my IntArrayRC.cpp or pg49.cpp
    > > reference
    > >   *   IntArray::IntArray(int)' at all.  how come compiler complain
    > > that?
    > >   */
    > > eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp IntArrayRC.cpp
    > > pg49.cpp
    > > /tmp/ccFlMagZ.o: In function `IntArrayRC::IntArrayRC(int*, int)':
    > > IntArrayRC.cpp:(.text+0x15): undefined reference to
    > > `IntArray::IntArray(int)'
    > > collect2: ld returned 1 exit status
    > > eric@eric-laptop:~/CppPrimer3$

    >
    > > looking to see any advancer's help again, and
    > > Thanks a lot in advance,

    >
    > You declared the constructor in 'IntArray' that takes a single 'int'
    > argument, but the argument has a default value, right?  So, that makes
    > it the default constructor.  Since 'IntArrayRC' does not initialize its
    > base class in any way, the compiler must use the default constructor.
    > And you didn't define it anywhere.  Define it, and make sure your
    > compiler knows where it is.
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Dear Victor Bazarov or any advanced c++ programer:

    then I add
    -------
    IntArray::IntArray(int size = DefaultArraySize)
    {
    _size = size;
    }
    -----
    to the end of my IntArray.cpp
    then still not work
    --------
    eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp IntArrayRC.cpp
    pg49.cpp
    IntArray.cpp:11:47: error: default argument given for parameter 1 of
    ‘IntArray::IntArray(int)’
    IntArray.h:7:13: error: after previous specification in
    ‘IntArray::IntArray(int)’
    ---------
    the only place I put (declare) IntArray(int) is in IntArray.h and in
    the sentence
    ---------
    explicit IntArray( int size =
    DefaultArraySize);
    in

    class IntArray {
    public:
    // equality and inequality operations: #2b
    explicit IntArray( int size = DefaultArraySize );
    IntArray( int *array, int array_size );
    IntArray( const IntArray &rhs );
    ------------

    so need your help again, Eric
    eric, Jun 9, 2011
    #4
  5. On 6/9/2011 4:01 PM, eric wrote:
    > On Jun 9, 10:44 am, Victor Bazarov<> wrote:
    >> On 6/9/2011 1:25 PM, eric wrote:
    >>
    >>
    >>
    >>> On Jun 9, 12:44 am, Paavo Helde<> wrote:
    >>>> eric<> wrote in news:33ca3f4e-bbef-4072-9fc6-
    >>>> :

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

    >>
    >>>> [...]

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

    >>
    >>>> With g++ this typically means you have not provided definition of the
    >>>> first non-inline virtual function in this class. As far as I can see,
    >>>> this would be IntArray::Sort(). Provide a definition of this function
    >>>> (and all other non-defined virtual functions) in the relevant .cpp file.

    >>
    >>>> In C++ one can in principle leave out definitions for functions which are
    >>>> not used, but all virtual functions are effectively "used" for populating
    >>>> the vtable entries, so they must be all present in the program.

    >>
    >>>> hth
    >>>> Paavo

    >>
    >>> /
    >>> ***********************************************************************************************/

    >>
    >>> Thanks Paavo and Brian your reply on this undefined reference

    >>
    >>> now I delete more variables, functions in my IntArray.h and add
    >>> IntArrayRC.cpp(and IntArray.cpp)
    >>> /
    >>> ***********************************************************************************************/
    >>> #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 _size = DefaultArraySize );
    >>> IntArrayRC( int *array, int array_size );
    >>> IntArrayRC( const IntArrayRC&rhs );
    >>> /* virtual*/ int& operator[]( int );

    >>
    >>> private:
    >>> void check_range( int );
    >>> };

    >>
    >>> #endif
    >>> /*----------------------------*/
    >>> // IntArrayRC.cpp

    >>
    >>> #include "IntArrayRC.h"

    >>
    >>> IntArrayRC::IntArrayRC(int *iarray, int iarray_size)
    >>> {
    >>> ia = iarray;
    >>> _size = iarray_size;
    >>> }
    >>> /*-------------------------------------------------------*/
    >>> // IntArray.cpp

    >>
    >>> #include "IntArray.h"

    >>
    >>> IntArray::IntArray(int *iarray, int iarray_size)
    >>> {
    >>> ia = iarray;
    >>> _size = iarray_size;
    >>> }

    >>
    >>> /*-------------------------------------------------------*/
    >>> /* my pg49.cpp and IntArray.cpp didn't change */
    >>> /*--------- ----------*/
    >>> /* I tried to make everything simple, just hope to make it run, but
    >>> it still not work, and I don't see my IntArrayRC.cpp or pg49.cpp
    >>> reference
    >>> * IntArray::IntArray(int)' at all. how come compiler complain
    >>> that?
    >>> */
    >>> eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp IntArrayRC.cpp
    >>> pg49.cpp
    >>> /tmp/ccFlMagZ.o: In function `IntArrayRC::IntArrayRC(int*, int)':
    >>> IntArrayRC.cpp:(.text+0x15): undefined reference to
    >>> `IntArray::IntArray(int)'
    >>> collect2: ld returned 1 exit status
    >>> eric@eric-laptop:~/CppPrimer3$

    >>
    >>> looking to see any advancer's help again, and
    >>> Thanks a lot in advance,

    >>
    >> You declared the constructor in 'IntArray' that takes a single 'int'
    >> argument, but the argument has a default value, right? So, that makes
    >> it the default constructor. Since 'IntArrayRC' does not initialize its
    >> base class in any way, the compiler must use the default constructor.
    >> And you didn't define it anywhere. Define it, and make sure your
    >> compiler knows where it is.
    >>
    >> V
    >> --
    >> I do not respond to top-posted replies, please don't ask

    >
    > Dear Victor Bazarov or any advanced c++ programer:
    >
    > then I add
    > -------
    > IntArray::IntArray(int size = DefaultArraySize)


    Say, do you *read* the error messages? Do you try to comprehend them?
    Or do you just copy-paste and post them here? Do you have a book that
    explains default arguments at all?...

    Drop the default value declaration from the function definition. Make
    it look like this:

    IntArray::IntArray(int size)

    > {
    > _size = size;


    Read the FAQ, section 10.

    > }
    > -----
    > to the end of my IntArray.cpp
    > then still not work
    > --------
    > eric@eric-laptop:~/CppPrimer3$ g++ IntArray.cpp IntArrayRC.cpp
    > pg49.cpp
    > IntArray.cpp:11:47: error: default argument given for parameter 1 of
    > ‘IntArray::IntArray(int)’
    > IntArray.h:7:13: error: after previous specification in
    > ‘IntArray::IntArray(int)’
    > ---------
    > the only place I put (declare) IntArray(int) is in IntArray.h and in
    > the sentence
    > ---------
    > explicit IntArray( int size =
    > DefaultArraySize);
    > in
    >
    > class IntArray {
    > public:
    > // equality and inequality operations: #2b
    > explicit IntArray( int size = DefaultArraySize );
    > IntArray( int *array, int array_size );
    > IntArray( const IntArray&rhs );
    > ------------
    >
    > so need your help again, Eric


    Let's make it your last before you try thinking...

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 9, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. brian

    Size of my Struct? PLZ PLZ reply

    brian, Nov 23, 2004, in forum: C Programming
    Replies:
    7
    Views:
    404
    -berlin.de
    Nov 25, 2004
  2. Replies:
    1
    Views:
    360
  3. Replies:
    2
    Views:
    319
    Nick Keighley
    Nov 24, 2006
  4. Replies:
    1
    Views:
    326
  5. nocturnal
    Replies:
    1
    Views:
    505
    nocturnal
    Jul 10, 2009
Loading...

Share This Page