0

My function prototype looks as follows:

// Purpose: finds an element in the ArrayList  

// Parameters: 'x' is value to be found in the ArrayList

// Returns: the position of the first occurrance of 'x' in the list, or -1  if 'x' is not found.

int find(const T& x) const;

It is placed in the class ArrayList

template <typename T>
class ArrayList
{ 
  private:  
    int m_size;                          // current number of elements
    int m_max;                           // maximum capacity of array m_data
    T* m_data;                           // array to store the elements

    T m_errobj;                          // dummy object to return in case of error


  public:
    int find(const T& x) const;

My definition is:

template <typename T>
int find(const T& x) const
{
  int i;
  while (m_data[i]!=x && m_data[i]!=NULL)
    i++;
  if (m_data[i]=x)
    return i;
  else
return (-1);
}

Whenever I compile, I receive the error in the title, and an error that m_data is not declared in the scope. How do I fix this?

Edit: I changed the definition to

 int ArrayList<T>:: find(const T& x) const

I got a ton of errors

int ArrayList:: find(const T& x) const

didn't work either

cjackson
  • 1
  • 2
  • Shouldn't it be inside the class? – user253751 Oct 01 '16 at 01:38
  • Ummm... Sounds like you are defining a class member function outside the class scope. You must qualify it with the class name like `ArrayList::find(....) const` .... And besides, you should be aware that you can't *really* [split template](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) header/implementation. – WhiZTiM Oct 01 '16 at 01:39
  • My function prototype is inside of a class ArrayList – cjackson Oct 01 '16 at 01:50
  • May you update your code? – amanuel2 Oct 01 '16 at 01:54

1 Answers1

1

Templates must be defined in headers. In your case, you are spliting it in .h/.cpp. In order to work, you need to define it together with your class definition. Something like this:

template <typename T>
class ArrayList
{ 
private:  
    int m_size;                          // current number of elements
    int m_max;                           // maximum capacity of array m_data
    T* m_data;                           // array to store the elements

    T m_errobj;                          // dummy object to return in case of error

public:
   int find(const T& x) const;
};

#include "x.hpp"

and define it in a file x.hpp

template <typename T>
int ArrayList<T>::find(const T& x) const
{
   int i;
   while (m_data[i]!=x && m_data[i]!=NULL)
   i++;
   if (m_data[i]=x)
      return i;
   else
      return (-1);
}

Note that this has the same effect as you have defined everything in a unique header file

Amadeus
  • 10,199
  • 3
  • 25
  • 31
  • 1
    This does not give me any errors. What is one error you got? Of course, if you try to use it, you will get all kinds of problems, but that's another issue. – Kenny Ostrom Oct 01 '16 at 03:04