I've implemented stack process.this program is supposed to work exactly the same as a real stack memory.moreover i'm trying to use Template and make the the program more generic. I've got a problem in using #define DEFAULT_SIZE 10
as the argument of class constructor.
First of all when i put DEFAULT_SIZE
in the prototype of the constructor it goes smoothly:
#define DEFAULT_SIZE 10
template<typename T>
class stack {
public:
stack(int size=DEFAULT_SIZE);
private:
T *elements;
int size;
int count;
};
template<typename T>
stack<T>::stack(int s) {
cout << "--constructor called\n";
size = s;
elements = new T[size];
count = 0;
}
But when I just put DEFAULT_SIZE
in outline definition of the class constructor i get this error: no appropriate default constructor available
#define DEFAULT_SIZE 10
template<typename T>
class stack {
public:
stack(int size);
private:
T *elements;
int size;
int count;
};
template<typename T>
stack<T>::stack(int s=DEFAULT_SIZE) {
cout << "--constructor called\n";
size = s;
elements = new T[size];
count = 0;
}
Finally the main of the program:
int main() {
stack<int> u;
u.push(4);
}
My question is not about "Why can templates only be implemented in the header file?" My problem is the place where I use DEFAULT_SIZE
.