By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,922 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,922 IT Pros & Developers. It's quick & easy.

ProblemWithTemplate

P: n/a
template<class T>
class Stack : public stack<T{
public:
T pop() {
T tmp = top();
pop();
return tmp;
}
};

is there anything wrong with my code above?
why doesn't it work?

Dec 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Magcialking wrote:
template<class T>
class Stack : public stack<T{
public:
T pop() {
T tmp = top();
T tmp = this->top();

or

T tmp = stack<T>::top();
pop();
this->pop();

or

stack<T>::pop();
return tmp;
}
};

is there anything wrong with my code above?
why doesn't it work?
I guess your problem is the compiler (supposedly) failed to look up
member functions from the base class since T is unknown by the time. You
need to qualify top() and pop().

If you have further problems please post error message.

Regards,
Ben
Dec 21 '06 #2

P: n/a
"Magcialking" <ma********@163.comwrote:
template<class T>
class Stack : public stack<T{
public:
T pop() {
T tmp = top();
pop(); <--- recursive function call
return tmp;
}
};

is there anything wrong with my code above?
why doesn't it work?
The program is calling Stack<T>::pop() from inside Stack<T>::pop().

The best way to fix is to not derive from stack. Better would be:

template < typename T >
T pop_return( stack<T>& s ) {
T result = s.top();
s.pop();
return result;
}

Note however, that this is not exception safe. That's why it is not in
the standard stack interface.
Dec 21 '06 #3

P: n/a
Daniel T. wrote:
"Magcialking" <ma********@163.comwrote:
>template<class T>
class Stack : public stack<T{
public:
T pop() {
T tmp = top();
pop(); <--- recursive function call
return tmp;
}
};

is there anything wrong with my code above?
why doesn't it work?

The program is calling Stack<T>::pop() from inside Stack<T>::pop().

The best way to fix is to not derive from stack. Better would be:

template < typename T >
T pop_return( stack<T>& s ) {
T result = s.top();
s.pop();
return result;
}

Note however, that this is not exception safe. That's why it is not in
the standard stack interface.
Yes - I remember about the non-exception safety but I can't remember how
it manifests itself in this case. Can you illuminate.

Dec 21 '06 #4

P: n/a
Gianni Mariani wrote:
Daniel T. wrote:
>"Magcialking" <ma********@163.comwrote:
>>template<class T>
class Stack : public stack<T{
public:
T pop() {
T tmp = top();
pop(); <--- recursive function call
return tmp;
}
};

is there anything wrong with my code above?
why doesn't it work?

The program is calling Stack<T>::pop() from inside Stack<T>::pop().

The best way to fix is to not derive from stack. Better would be:

template < typename T >
T pop_return( stack<T>& s ) {
T result = s.top();
s.pop();
return result;
}

Note however, that this is not exception safe. That's why it is not in
the standard stack interface.

Yes - I remember about the non-exception safety but I can't remember how
it manifests itself in this case. Can you illuminate.
If the copying of the return value raises an exception you will miss out
that value and you can't find it from the container because it is deleted.

Ben
Dec 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.