【问题标题】:Lists containers in c++ , using a list in a class在 c++ 中列出容器,使用类中的列表
【发布时间】:2025-11-26 10:50:01
【问题描述】:

我正在使用列表创建此模板队列。我不明白列表部分的错误是什么。当我键入 listObject.push_back() 时,程序说没有找到任何成员函数。提前致谢

#include <iostream>
#include <list>


using namespace std;


template<class T, class E>
class Queue{

public:

// Creates a Queue with an initial capacity of 10
Queue();
//Creates a Queue object with an initial capacity of size cap
Queue(int cap);
//adds item to the end of the queue
void add(T item);
//returns true if the queue is empty
bool isEmpty() const;
//removes the front item from the qeue
T remove();
//Provide a copy of the object that is first in the queue
T first() const;
//updates the item in the front of the queue
void updateFirst(T item);
//output all information currently stored in the queue
friend ostream& operator<<(ostream& out, const Queue<E>& obj)

 private:

list<E> listObject;
int capacity;
};

 template<class T, class E>
 Queue<T,E>::Queue()
 {
capacity = 10;
 }

 template<class T, class E>
 Queue<T,E>::Queue(int cap)
 {

capacity = cap;
 }

 template<class T, class E>
  void Queue<T,E>::add(E item)
 {

listObject.push_back( item );

 }  

【问题讨论】:

  • 好吧,您正在T 推送到Es 列表中。

标签: c++ list templates stl containers


【解决方案1】:

您的列表对象被定义为 list&lt;E&gt;,但您正试图 push_back 类型为 T 的对象。

【讨论】:

  • 谢谢。但即使我更正了它,当我输入 listObject.程序说没有可用的成员函数。
  • “程序”是编译器,您应该粘贴准确的错误信息。
  • 当你说“程序说没有可用的成员函数”时,我不明白你的意思。我将您的代码复制到一个文件中,修复了几个语法错误,然后编译没有错误。
  • 我明白了。谢谢大家。