invalid use of template-name 'Array' without an argument(compileerror, plz help)

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

  1. eric

    eric Guest

    #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
    --------------------------------------------------------------------------------------------------------------
    // Array.cpp

    #include "Array.h"

    /*
    Array::Array(elemType *iarray, int iarray_size)
    {
    ia = iarray;
    _size = iarray_size;
    }
    */

    Array::Array(const Array &rhs )
    {
    ia = rhs;
    }

    Array::Array(int size )
    {
    _size = size;
    }


    --------------------------------------------------------------------------------
    #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;
    }
    ------------------------------------------
    but my compiler result is
    ------------
    eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp
    Array.cpp:13:1: error: invalid use of template-name ‘Array’ without an
    argument list
    Array.cpp:18:1: error: invalid use of template-name ‘Array’ without an
    argument list
    eric@eric-laptop:~/CppPrimer3$
     
    eric, Jun 10, 2011
    #1
    1. Advertisements

  2. Should probably be

    FAQ section 10. Prefer initialization over assignment.
    Same here. You're defining a constructor of a class template, and you
    need the 'template' declaration:

    No. 'Array' is a template-id. In order to use it you need the template
    argument list (like you did in 'main' when declaring an object of a
    concrete type).
    Also, please read the FAQ section 35 (if memory serves). You're likely
    to encounter linker errors since the definitions of the member functions
    of your 'Array' template are not available to the compiler at the time
    when it needs it (while compiling the 'main' function).

    V
     
    Victor Bazarov, Jun 10, 2011
    #2
    1. Advertisements

  3. eric

    eric Guest

    ------------------------------------------------------------------------------
    I follow your suggestion modify my Array.cpp file, so now it become
    -----
    // 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(int size )
    {
    _size = size;
    }

    ----------------------------------------------
    I also modify a liitle bit of my Array.h
    -------------------------------------------
    #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
    -------------------------------------------------------------
    but my compile result is still not success
    -----------
    eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp
    /tmp/ccBUveZE.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
    -------------------------------------------------------------
    I know it is these statements didn't match declaration
    ---
    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;
     
    eric, Jun 10, 2011
    #3
  4. eric

    eric Guest

     
    eric, Jun 11, 2011
    #4
  5. ^^^^
    That's the linker. You got the linker errors as I said you would.
    Go read the FAQ.

    V
     
    Victor Bazarov, Jun 11, 2011
    #5
  6. eric

    Öö Tiib Guest

    Both feel wrong. operator== is generally not expected to modify its
    operands so usually the choice is:

    1) bool X::eek:perator==(X const& rhs) const;
    2) bool operator==(X const& lhs, X const& rhs);

    Several coding standards suggest to write binary operators outside of
    class also the result of comparison likely depends only on externally
    visible state. So (2) is more favored in general, but both are correct.
     
    Öö Tiib, Jun 13, 2011
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.