Mail Archives: djgpp/1997/04/25/05:29:24
I am currently working on using a template in my program to make a
dynamic 1 and 2 dimensional array of any type. Whenever I try to compile
the program the program will compile but, when it tries to link the
program into an executable I get unrecognized fuction errors for every
single function that I have that is part of the template.
I tried originally having two files a cpp and an h file but, when I
changed it to a template it didn't work. I looked at some examples in
some C++ books that I have and they had everything in the header file so
I put everything in my header file also. This still did not work. Even
the books example did not work.
Is there a specific way that I have to include files to make them
templates or some syntax that I am missing. Included below is a small
example that shows the code for the 1D dynamic array.
main.cpp
#include "array.h"
void main(void)
{
ARRAY<int> temp(10);
cout << "Width " << temp.Width() << endl;
temp.Enter(10, 5);
cout << "Result " << temp.Return(10) << endl;
};
array.h
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream.h>
template <class Type>
class ARRAY
{
public:
ARRAY(int width_);
void Enter(int width_, Type c);
Type Return(int width_);
int Width(void);
int Height(void);
private:
int width_;
int height_;
int width;
int height;
Type c;
Type *array;
};
template <class Type>
ARRAY<Type>::ARRAY(int width_)
{
width = width_;
array = new Type [width];
assert(array != 0);
}
template <class Type>
void ARRAY<Type>::Enter(int width_, Type c)
{
array[width_ - 1] = c;
}
template <class Type>
Type ARRAY<Type>::Return(int width_)
{
return array[width_ - 1];
}
template <class Type>
int ARRAY<Type>::Width(void)
{
return width;
}
#endif
- Raw text -