Hello,
Thanks in advance for anyone's help and/or advice, it is greatly apprecaited on what, i am sure, is a stupid question. Nevertheless, here is my problem:
I have a template class called "MyArray" and i want it to be able to accept an int or char. The contents of the array and the size of the array is taken as a command line argument.
Here is the code in the main function:
Code:
#include "MyArrayDefs.h"
using namespace std;
void main( int argc, char* argv[] )
{
MyArray<char> charArr( strlen( argv[ 1 ] ) );
MyArray<int> intArr( strlen( argv[ 1 ] ) );
}
Here is the header file:
Code:
template < class Type > class MyArray
{
public:
MyArray( const int );
~MyArray();
private:
char* base;
int size;
};
Here is the Template header file:
Code:
#include <iostream>
#include "MyArray.h"
using namespace std;
template < class Type >
MyArray < Type > :: MyArray( const int n )
{
base = new Type [ n ];
size = n;
}
When i create an array like this:
Code:
MyArray<char> charArr( strlen( argv[ 1 ] ) );
it works fine. But when i do this:
Code:
MyArray<int> intArr( strlen( argv[ 1 ] ) );
it does not work fine. It comes up with the following error:
MyArrayDefs.h: In method `MyArray<int>::MyArray(int)':
main.cc:15: instantiated from here
MyArrayDefs.h:14: assignment to `char *' from `int *'
Any help in solving this error would be massively appreciated.
Best regards, global