C++模板链表双栈报错:'node'无合适默认构造函数求解
问题:基于单链表的模板化栈编译错误:
'node': no appropriate default constructor available 我正在完成课堂作业,实现一个基于单链表的模板化栈,要求同时压入(push)和弹出(pop)两个元素。编译时两次出现错误提示:'node': no appropriate default constructor available。我不确定是否需要为node类添加默认构造函数,也不知道该如何处理,附上我的.h和.cpp文件,希望能得到帮助。
.h 文件
#ifndef _DOUBLESTACKLIST_H #define _DOUBLESTACKLIST_H #include <iostream> using namespace std; template<class ItemType> class node { public: ItemType data; node* next; }; template <class ItemType> class DoubleStackList { private: node<ItemType>* top; int length; node<ItemType>* currentPos; public: DoubleStackList(); //DoubleStackList(int max); bool isEmpty(); bool isFull(); void push(ItemType item1, ItemType item2); void pop(); ItemType topView() const; void printStack() const; }; #include "DoubleStackList.cpp" #endif
.cpp 文件
#ifndef _DOUBLESTACKLIST_CPP #define _DOUBLESTACKLIST_CPP #include "DoubleStackList.h" template<class ItemType> DoubleStackList<ItemType>::DoubleStackList() { length = 0; top = NULL; currentPos = NULL; } template<class ItemType> inline bool DoubleStackList<ItemType>::isEmpty() { return(length == 0); //or top == NULL } template<class ItemType> inline bool DoubleStackList<ItemType>::isFull() { node* location; try { location = new node; delete location; return false; } catch (std::bad_alloc exception) { return true; } } template<class ItemType> void DoubleStackList<ItemType>::push(ItemType item1, ItemType item2) { if (isFull()) cout << "Stack is full and no more items can be added" << endl; else { // Item 1 node* temp = new node; temp->data = item1; temp->next = top; top = temp; length++; // check if item stack is full, if it's not then another can be pushed in so it can push two at the same time if (isFull()) cout << "No more items can be added into the stack." << endl; else { // Item 2 temp = new node; temp->data = item2; temp->next = top; top = temp; length++; } } } template<class ItemType> void DoubleStackList<ItemType>::pop() { if (isEmpty()) cout << "Stack is Empty and item cannot be popped" << endl; else { node* temp; temp = top; top = top->next; delete temp; length--; //we check again to see if the stack is empty if one item is popped, if it's not then we pop another so two elements get popped out of the array if (isEmpty()) cout << "Stack is empty, no items can be popped out!" << endl; //now we remove another by decrementing another so two elements will be popped else { node* temp; temp = top; top = top->next; delete temp; length--; } } } template<class ItemType> ItemType DoubleStackList<ItemType>::topView() const { if (!isEmpty()) // if list is not empty get the top's data so it shows the data { return top->data; } else // if it's empty it will display msg { cout << "Stack is Empty and nothing at the top" << endl; return NULL; } } template<class ItemType> void DoubleStackList<ItemType>::printStack() const { node* temp; //check if the stack is empty if (length == 0) { //top == NULL cout << "Stack is Empty."; } else { temp = top; while (temp != NULL) { // print data of the node cout << temp->data; // assign temp link to temp temp = temp->next; if (temp != NULL) cout << " -> "; } } } #endif
问题分析与解决方法
错误原因
执行new node创建节点时,编译器需要调用node类的默认构造函数。你的node类仅声明成员变量,未显式定义构造函数:
- 若
ItemType是自带默认构造的类型(如int、std::string),编译器会自动为node生成默认构造; - 但如果
ItemType是自定义类且仅定义带参构造(无默认构造),编译器无法生成合法的node默认构造,触发报错。
解决方案(二选一即可)
方案1:给node类添加构造函数
显式定义默认构造和带参构造,既解决编译问题,还能优化节点创建逻辑:
template<class ItemType> class node { public: ItemType data; node* next; // 默认构造:初始化next为空指针,避免野指针 node() : next(nullptr) {} // 带参构造:直接初始化data和next,省去后续赋值步骤 node(const ItemType& val, node* nxt = nullptr) : data(val), next(nxt) {} };
修改后,push函数的节点创建可简化为:
// 替代原来的new node + 赋值 node<ItemType>* temp = new node<ItemType>(item1, top); top = temp; length++;
方案2:创建节点时直接初始化成员
若不想修改node类定义,可在new时用初始化列表直接初始化data和next,无需依赖默认构造(要求C++11及以上标准):
// 替换原来的`node* temp = new node; temp->data = item1; temp->next = top;` node<ItemType>* temp = new node<ItemType>{item1, top};
额外需要修正的问题
- 模板参数缺失:所有
node*需改为node<ItemType>*,比如isFull函数中的node* location;,否则编译器无法识别模板实例。 topView返回值问题:return NULL;仅适用于指针类型的ItemType,若为int等非指针类型会导致类型不匹配。建议改为返回ItemType()(默认构造对象),或调整函数设计(如返回bool,通过输出参数传递top值)。pop函数提示优化:当栈中仅1个元素时,弹出第一个后栈为空,此时第二个弹出操作被跳过,提示信息可改为"Stack now empty after popping one element."更准确。
内容的提问来源于stack exchange,提问作者Alexa Demao
相关产品推荐
相关产品推荐

