Using a generic stack class

Hi there, I'm trying out templates, and I've been getting the following compiler error where I declare a template:

Stack2.h:5: error: expected '>' before numeric constant

Can someone please point me in the right direction?



/* stack2.h */

#define CAPACITY 100

template <typename T, size_t CAPACITY = 100>
class Stack {
private:
T data_[CAPACITY]; // an array of 100 elements
size_t size_; // the max number of elements
public:
// precondition: full() must be false
void push(int n) { // puts n into the stack
data_[size_++] = n;
}

T top() { // returns the array value that is on top of the stack
return data_[size_];
}

bool full() {
return (size_ == CAPACITY-1);
}

void pop() {
size_--;
}

bool empty() {
return (size_ == 0);
}

size_t size() {
return size_;
}

Stack():size_(0) { } // member initializer size_t = 0

}; // end class stack


/* stack2.cpp */

#include <iostream>
#include "Stack2.h"
using namespace std;

int main() {

Stack<long, CAPACITY> s; // a variable of type Stack

// populate the stack with numbers 1-100
if (!s.full())
for (long i = 0; i < CAPACITY; i++)
s.push(i);

// display the data
while (!s.empty()){
s.pop();
cout << s.top() << endl;
}

return 0;
} // end function main
It's because of this: #define CAPACITY 100
which leads to template <typename T, size_t 100 = 100>

so either you rename the #define or the template parameter
Thanks, coder777, that was exactly it. I changed the template parameter.
Topic archived. No new replies allowed.