0

I'm in the early stage of trying to convert an array class containing operator overloading into a templatized class. At the moment I'm trying to add the template definition to the class and each function. This should be fairly simple however, whenever I run the program I get a scope error.

The compiler says `T' was not declared in this scope (I will comment the error on the line that it occurs). The error also re-occurs on the other function definitions. I'm using a program invovling a templatized class as a guide and it implements the functions in the exact manner that I am trying to(which is why I'm confused). What do I need to do to resolve this?

Thank you.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdexcept>
using namespace std;

#ifndef ARRAY_H
#define ARRAY_H

template <typename T>
class Array
{
  public:
         Array(int = 10);
         Array(const Array&);
         ~Array();
         int getSize() const;

         const Array &operator=(const Array &);
         bool operator==(const Array&) const;

         bool operator!=(const Array &right) const
         {
              return ! (*this == right);     
         }     

         int &operator[](int);
         int operator[](int) const;
  private:
          int size;
          int *ptr;        
};

#endif

template<typename t>
Array<T>::Array(int arraySize) //ERROR: T was not declared in this scope***********
{
 if(arraySize > 0)
              size = arraySize;
 else
     throw invalid_argument("Array size myst be greater than 0");

 ptr = new int[size];

 for(int i = 0; i < size; i++)
              ptr[i] = 0;   
}

template<typename t>
Array<T>::Array(const Array &arrayToCopy): size(arrayToCopy.size)
{
 ptr = new int[size];

 for(int i = 0; i < size; i++)
         ptr[i] = arrayToCopy.ptr[i];                  
}

template<typename t>
Array<T>::~Array()
{
 delete [] ptr;              
}

template<typename t>
int Array<T>::getSize() const
{
 return size;   
}

template<typename t>
const Array<T> &Array::operator=(const Array &right)
{
 if (&right != this)
 {
    if(size != right.size)
    {
            delete [] ptr;
            size = right.size;  
            ptr = new int[size];
    }

 for(int i = 0; i < size; i++)
         ptr[i] = right.ptr[i]; 
 }  

 return *this;
}

template<typename t>
bool Array<T>::operator==(const Array &right) const
{
 if(size != right.size)
         return false;

 for(int i = 0; i < size; i++)
         if(ptr[i] != right.ptr[i])
                   return false;

 return true;     
}

template<typename t>
int &Array<T>::operator[](int subscript)
{
 if(subscript < 0 || subscript >= size)
              throw out_of_range("Subscript out of range");

 return ptr[subscript];   
}

template<typename t>
int Array<T>::operator[](int subscript) const
{   
    if(subscript < 0 || subscript >= size)
                 throw out_of_range("Subscript out of range");

    return ptr[subscript];
}

int main()
{
    //main is empty at the moment because I want to make sure that the class is functional
    //before implementing the driver function.

    system("pause");  
}
2
  • Never do "using namespace ..." in a header. This is considered extremely bad form. Commented Mar 26, 2013 at 22:59
  • It's not really a header. I'm just using one .cpp file at the moment, until I seperate it. Commented Mar 27, 2013 at 12:35

1 Answer 1

5

Before each of your function definitions, you have written:

template<typename t>

You mean:

template<typename T>

That is, it should match the template parameter of your class, which is a capital T.

Sign up to request clarification or add additional context in comments.

3 Comments

Also most of the ints should be replaced by T.
@drescherjm I think he hasn't gotten to that part yet.
Wow. Thank you, sometimes you just need someone else to look at your code, right? @drescherjm: that's my next step, to switch everything to template parameters that need to be one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.