Class template error
Apr 6, 2014 at 7:43am UTC
I got an array class, but I'm getting this error:
Error 1 error C2953: 'Array2D' : class template has already been defined
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include "Tools.h"
template <typename T>
class Array2D
{
T** arr;
int xSize;
int ySize;
public :
Array2D(int xSize, int ySize)
{
arr = Tools::allocateDynamicArray<T>(xSize, ySize);
this ->xSize = xSize;
this ->ySize = ySize;
for (int i = 0; i<xSize; i++)
{
for (int j = 0; j<ySize; j++)
{
arr[i][j] = null;
}
}
}
int getXSize()
{
return xSize;
}
int getYSize()
{
return ySize;
}
T get(int i, int j)
{
return arr[i][j];
}
void set(int i, int j, T value)
{
arr[i][j] = value;
}
Array2D(void );
~Array2D(void );
};
Apr 6, 2014 at 7:56am UTC
can we see Tools.h? other than that, the only thing i can think of is the multiple constructors which shouldnt be an issue
Apr 6, 2014 at 7:58am UTC
Tools.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#pragma once
#include <vector>
class Tools
{
public :
template <typename T>
static T **allocateDynamicArray( int nRows, int nCols)
{
T **dynamicArray;
dynamicArray = new T*[nRows];
for ( int i = 0 ; i < nRows ; i++ )
dynamicArray[i] = new T [nCols];
return dynamicArray;
}
static long generateRandomLong();
static bool isPrime(int num);
static int randomNumberRange(int min, int max);
};
Apr 6, 2014 at 7:12pm UTC
each header should have header guard:
file Tools.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#ifndef _TOOLS_H
#define _TOOLS_H
#include <vector>
class Tools
{
public :
template <typename T>
static T **allocateDynamicArray( int nRows, int nCols)
{
T **dynamicArray;
dynamicArray = new T*[nRows];
for ( int i = 0 ; i < nRows ; i++ )
dynamicArray[i] = new T [nCols];
return dynamicArray;
}
static long generateRandomLong();
static bool isPrime(int num);
static int randomNumberRange(int min, int max);
};
#endif /* _TOOLS_H */
Topic archived. No new replies allowed.