构建模板 使用矢量 c++ 的堆栈结构
Building template Stack structure with vector c++
我在C++的向量基础上实现了模板化的堆栈结构。我不确定我的代码出了什么问题。
Stack.h
#ifndef STACK_H_
#define STACK_H_
#include <vector>
using namespace std;
template <class T>
class Stack {
public:
Stack();
virtual ~Stack();
bool empty();
int size();
void push(T pushvar);
T pop();
private:
std::vector<T> container;
};
#endif /* STACK_H_ */
堆栈.cpp
#include "Stack.h"
#include <vector>
//template <class T>
Stack::Stack()
:container(0)
{
}
Stack::~Stack() {
// TODO Auto-generated destructor stub
}
bool Stack::empty(){
return (container.empty());
}
甚至在从主调用任何内容之前,Eclipse 就给了我几个错误。但我会给出一个示例性的主要:
#include <iostream>
#include "Stack.cpp"
using namespace std;
int main() {
Stack<int> s;
cout << s.empty();
return (0);
}
编译器返回以下错误:
Description Resource Path Location Type
'container' was not declared in this scope Stack.cpp /PracticeCpp/src line 23 C/C++ Problem
'template<class T> class Stack' used without template parameters Stack.cpp /PracticeCpp/src line 22 C/C++ Problem
invalid use of template-name 'Stack' without an argument list Stack.cpp /PracticeCpp/src line 12 C/C++ Problem
invalid use of template-name 'Stack' without an argument list Stack.cpp /PracticeCpp/src line 18 C/C++ Problem
Member declaration not found Stack.cpp /PracticeCpp/src line 12 Semantic Error
Member declaration not found Stack.cpp /PracticeCpp/src line 18 Semantic Error
Member declaration not found Stack.cpp /PracticeCpp/src line 22 Semantic Error
Method 'empty' could not be resolved Stack.cpp /PracticeCpp/src line 23 Semantic Error
Symbol 'container' could not be resolved Stack.cpp /PracticeCpp/src line 13 Semantic Error
我知道我还没有意识到头文件中声明的所有方法,但这不是我的问题。在我继续意识到它们之前,我想了解我错在哪里?
答案后的更正:
我遵循了答案中的建议,但我仍然不明白继续出错的地方。我将模板实现移动到标题中。我删除了其他未实现的方法以避免混淆。现在我的.cpp文件是空的。我的新头文件:
#ifndef STACK_H_
#define STACK_H_
#include <vector>
template <class T>
class Stack {
private:
std::vector<T> container;
public:
template <typename T>
Stack<T>::Stack() : container(0)
{
}
template <class T>
bool Stack::empty() {
return container.empty();
}
};
#endif /* STACK_H_ */
定义应该是
template <typename T>
Stack<T>::Stack() : container(0)
{
}
而不是.cpp
方法Stack::empty()
从未声明,您声明Stack<T>::empty()
。对于每个构造函数、运算符和方法也是如此。向每个实现添加模板声明以修复此错误。请注意,错误消息通过说"在没有参数列表的情况下无效使用模板名称'堆栈'来暗示错误。
例:
template<class T> bool Stack<T>::empty() {
return container.empty();
}
模板方法的实现应包含在头文件中。看到这个问题。
编辑:
关于您的最新示例,您混淆了两种解决方案。请尝试以下任一操作:
#include <vector>
template <class T>
class Stack {
private:
std::vector<T> container;
public:
Stack() : container(0)
{
}
bool empty() {
return container.empty();
}
};
或
#include <vector>
template <class T>
class Stack {
private:
std::vector<T> container;
public:
Stack();
bool empty();
};
template<class T>
Stack<T>::Stack() : container(0)
{
}
template<class T>
bool Stack<T>::empty()
{
return container.empty();
}
在第一个解决方案中,您将在类的定义中定义函数。编译器知道你在做类Stack<T>
,你一定不能提醒它。在第二个解决方案中,函数在类外部定义。在这里,您必须指定要定义的类empty
方法和构造函数。
相关文章:
- 算法问题:查找从堆栈中弹出的所有序列
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么调用堆栈数组会导致内存泄漏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的 int main() 中出现堆栈溢出错误
- 堆栈和队列是否像C++中的数组一样传递?
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 为什么 STL 容器适配器堆栈中的 top 返回常量引用?
- 从堆栈分配的原始指针构造智能指针
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- 堆栈中大小变量输入错误 (C++)
- 堆栈问题(平衡表达式问题集)
- C++ 在堆栈中包含多态属性的类对象存储
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 在 C++ 中使用链表进行堆栈
- 变量周围的堆栈'...'已损坏
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- C++线程的可用堆栈大小