0

I get errors for the below code when I try to compile that tell me the following:

  • 'isEmpty' : is not a member of '_Stack'

  • Error while compiling class template member function 'void QueueS::enqueue(const T &)

    #include <iostream>
    #include <stack>
    using namespace std;
    
    template <class T>
    class _Stack : public stack<T> {
    
    public:
    
       T pop(){
           T tmp=stack::top();
           stack::pop();
           return tmp;
       }
    };
    
    template <class T>
    class QueueS {
    public:
        QueueS(){}
    
       bool isEmpty() const{
    
       }
    
    
       void enqueue(const T& el){
    
        while (!output.isEmpty()) {
             input.push(ouput.pop());
        }
    
        input.push(el);
    
        }
    
     private:
    
     _Stack<T> input;
     _Stack<T> output;
    
    };
    

I'm not sure what's going on. Can anyone offer any assistance? I have obviously not implemented isEmpty yet. Was wondering if that should have an effect.

Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • 4
    One problem is that `'isEmpty' : is not a member of '_Stack'`. This is nothing to do with templates. – juanchopanza Jun 08 '13 at 23:54
  • 1
    What do you think `stack::top()` returns? `top` is a member function and it should be called off an *instance* of `std::stack`. – David G Jun 08 '13 at 23:54
  • 1
    Unrelated to your problem, but still: You shouldn't use identifiers that start with underscore and uppercase letter (such as `_Stack`). See [here](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for a related SO post. – jogojapan Jun 09 '13 at 00:08
  • 1
    Better indentation would help to see that `isEmpty` is clearly defined outside the `_Stack` template. – jogojapan Jun 09 '13 at 00:10
  • Sorry; the indentation is clearer in my own code, but I always run into difficulty trying to paste something in the given code brackets here. – Rome_Leader Jun 09 '13 at 00:18
  • Would have tried to help if only this code was more readable, it's impossible to know what is going on... – Ravid Goldenberg Jun 09 '13 at 00:20
  • I'll try to fix it up some more. Again, my apologies. – Rome_Leader Jun 09 '13 at 00:23
  • Indentation should be better now. I see the point now about the isEmpty() usage. I should probably be using the library defined .empty instead. Still I'm confused about the latter error. – Rome_Leader Jun 09 '13 at 00:29

1 Answers1

1

What is it you are trying to do? That wont work since isEnpty is a member function of the class _stack. You can solve the first problem by adding the function isEmpty to your _Stack class.

Like so:

#include <iostream>
#include <stack>
using namespace std;

template <class T>
class _Stack : public stack<T> 
{

public:

T pop(){
T tmp=stack::top();
stack::pop();
return tmp;
}
bool isEmpty() const{

return stack::empty();
}
};

template <class T>
class QueueS {


public:
QueueS(){}

bool isEmpty() const{

}


void enqueue(const T& el){

while( !output.isEmpty()) {
input.push(output.pop());
}

input.push(el);

}

 private:
 _Stack<T> input;
 _Stack<T> output;

};


int main()
 {
 _Stack<int> sk;
 sk.isEmpty();
 QueueS<int> qu;
 qu.enqueue(4);
 return 1;
 }
Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59
  • I was just trying to use it as a check. Since my stacks are private, I was going to attempt to use the function (once written) as a go between for accessing them. Since they're declared in separate classes, though, might I be better off just using the library's .empty() check? – Rome_Leader Jun 09 '13 at 00:33
  • I'd still need an instance of the object to make that change, wouldn't I? I "get use of class template requires template argument list", and I believe that's what it is saying. – Rome_Leader Jun 09 '13 at 12:31
  • I am not quite sure what do you mean, maybe if you will add your main or the line that gave you the error. – Ravid Goldenberg Jun 09 '13 at 15:54
  • The line return std::empty() gives the error use of class template requires template argument list. – Rome_Leader Jun 09 '13 at 17:27
  • I still don't know what do you mean, I re edited the post with a main and this worked fine where is the problem? – Ravid Goldenberg Jun 09 '13 at 17:42
  • Verified your solution! My compiler was stuck on some garbage I had introduced in a previous solution. All good, thanks! – Rome_Leader Jun 09 '13 at 22:08