Reputation: 12899
suppose having a Template stack<class T, int maxElements
which have a subclass Entry
, which will build the Linked List for the stack, so something like this:
template <class T, int maxElements>
class Stack{
private:
class Entry{
friend class Stack;
T info;
Entry* next;
};
Entry* first;
public:
template<class U, int n>
void append(const Stack<U, n>& s){
if(isEmpty()) first = s.first; /* here is the problem*/
....
}
};
So, the problem is in the line marked, and it's assigning to 'Stack<char, 100>::Entry *' from incompatible type 'Stack<char, 10>::Entry *const'
, and that because it's building a "class" Entry for each template instantiation, but the point is that Entry doesn't depends from the maxElement
parameter, so I would like to know if there is a way to tell this to the compiler.
So far, the only way i can think that can do this, is to take out the class from the template, and making itself a template based only on T
PS: i know i'm sharing memory in the line where i'm having the error, one thing at time
Upvotes: 0
Views: 61
Reputation: 17415
The problem is that different template instantiations are different types. With that, nested types (Entry
in this case) are different types as well.
The solution is just as simple: Move parts to a (private) baseclass that only depends on the type:
template<typename T>
class StackBase {
protected:
struct Entry {
T info;
Entry* next;
};
};
Then you derive from this baseclass:
template<typename T, int maxElements>
class Stack: private StackBase<T> {
...
};
Upvotes: 2