【问题标题】:Template Membership Problems?模板成员问题?
【发布时间】:2013-06-04 23:58:21
【问题描述】:

当我尝试编译时收到以下代码的错误,告诉我以下内容:

  • 'isEmpty' : 不是 '_Stack' 的成员

  • 编译类模板成员函数'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;
    
    };
    

我不确定发生了什么。任何人都可以提供任何帮助吗?我显然还没有实现 isEmpty 。想知道这是否应该有效果。

【问题讨论】:

  • 一个问题是'isEmpty' : is not a member of '_Stack'。这与模板无关。
  • 您认为stack::top() 会返回什么? top 是一个成员函数,它应该在std::stackinstance 中调用。
  • 与您的问题无关,但仍然:您不应使用以下划线和大写字母开头的标识符(例如_Stack)。请参阅here 以获取相关的 SO 帖子。
  • 更好的缩进将有助于看到isEmpty_Stack 模板之外明确定义。
  • 对不起;缩进在我自己的代码中更清晰,但我总是在尝试在给定的代码括号中粘贴一些内容时遇到困难。

标签: c++ templates


【解决方案1】:

你想做什么? 这不会起作用,因为 isEnpty 是 _stack 类的成员函数。 您可以通过将函数 isEmpty 添加到您的 _Stack 类来解决第一个问题。

像这样:

#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;
 }

【讨论】:

  • 我只是想把它用作支票。由于我的堆栈是私有的,因此我将尝试使用该函数(一旦编写)作为访问它们的中间人。但是,由于它们是在单独的类中声明的,所以我最好只使用库的 .empty() 检查吗?
  • 我仍然需要一个对象的实例来进行更改,不是吗?我“使用类模板需要模板参数列表”,我相信这就是它的意思。
  • 我不太清楚你的意思是什么,也许你会添加你的 main 或给你错误的行。
  • return std::empty() 行给出了错误使用类模板需要模板参数列表。
  • 我还是不明白你的意思,我重新编辑了一个主要的帖子,这很好,问题在哪里?
猜你喜欢
  • 2010-12-08
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多