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

E

eric

#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$
 
V

Victor Bazarov

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

Should probably be

template said:
{
ia = rhs;

FAQ section 10. Prefer initialization over assignment.
}

Array::Array(int size )

Same here. You're defining a constructor of a class template, and you
need the 'template' declaration:

template said:
{
_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$

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).
so idea what these errors talk about, plz help, thank a lot in
advance, Eric

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
 
E

eric

#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;
};
#include "Array.h"
/*
Array::Array(elemType *iarray, int iarray_size)
{
   ia = iarray;
   _size = iarray_size;
}
*/
Array::Array(const Array&rhs )

Should probably be

template said:
{
   ia = rhs;

FAQ section 10.  Prefer initialization over assignment.
Array::Array(int size )

Same here.  You're defining a constructor of a class template, and you
need the 'template' declaration:

template<class T> Array<T>::Array(int size)


{
   _size = size;
}
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’ withoutan
argument list
Array.cpp:18:1: error: invalid use of template-name ‘Array’ withoutan
argument list
eric@eric-laptop:~/CppPrimer3$

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).
so idea what these errors talk about, plz help, thank a lot in
advance, Eric

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

eric

io_x said:
2 try; some error found, don't know if it compile

3 try, there is no 2 without 3

#include <stdint.h>
#include <stdlib.h>
#define  u32  uint32_t
#define  u8   uint8_t

template < class elemType >
class Array {
public:
  // parameterize element type
  Array(u32 size = 0)
  {if(size>0xFFFF)
     {ia_=0; size_=-1;}
   else if(size==0)
     {ia_=0; size_= 0;}
   else {u32  i;
         size_=-1; ia_=0;
         if(sizeof(elemType)>0xFFF) return;
         ia_=(elemType*) malloc(size* sizeof(elemType));
 cout << (void*)ia_;
 cout << " here in Array() elementsz= " << sizeof(elemType) << "\n";
         if(ia_==0) return;
         size_=size;
         for(i=0; i<size_; ++i)
                ia_=0;
        }
  }

  elemType& operator[](u32 index){ return ia_[index]; }

  Array(Array& rhs )
  {if(rhs.size_==-1||sizeof(*(rhs.ia_))!=sizeof(elemType))
        {size_=-1; ia_=0; return;}
   else {u32  i;
         size_=-1; ia_=0;
         if(rhs.size_>0xFFFF) return;
         if(sizeof(*(rhs.ia_)) > 0xFFF) return;
         ia_=(elemType*) malloc(rhs.size_ * sizeof(elemType));
         cout << (void*)ia_;
         cout << " sizeelm==" << sizeof(elemType) << "\n";
         if(ia_==0) return;
         size_=rhs.size_;
         for(i=0; i<size_; ++i)
            ia_=rhs.ia_;
        }
  }

 ~Array(){ cout << "~" << (void*)ia_ << "\n";  free(ia_);}

  u32 operator==(Array& a)
  {u32  i;
   if(sizeof(*(rhs.ai))!=sizeof(elemType))
             return 0;
   if(a.size_!=size_)
             return 0;
   for(i=0; i<size_; ++i)
     {if(a!=*this) return 0;
     }
   return 1;
  }

  u32 operator!=(Array& a){return  !(*this==a)}

  u32 size(void){return size_; }

  u32     size_;
  elemType*  ia_;

};

#include <iostream.h>

int main(void)
{u32 array_size=4;
 u32           ix;
 Array<u32>    ia(array_size);     // elemType becomes u32
 Array<double> da(array_size);     // elemType becomes double
 Array<char>   ca(array_size);     // elemType becomes char
 Array<u32>   iia(ia);

 if(ia.size_==(u32)-1||da.size_==(u32)-1||ca.size_==(u32)-1||
   iia.size_==(u32)-1)
     {cout << "Errore memoria insufficiente\n";
      return  0;
     }

 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] << "\n";
 return 0;

}
 
V

Victor Bazarov

[..]
so idea what these errors talk about, plz help, thank a lot in
advance, Eric

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).
[..]
I follow your suggestion modify my Array.cpp file, so now it become
[..]
-------------------------------------------------------------
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
^^^^
That's the linker. You got the linker errors as I said you would.
Go read the FAQ.

V
 
Ö

Öö Tiib

so what is better to use
"u32 operator==(Array& a)" or "u32 operator==(Array& a, Array& b)"?

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top