构建模板 使用矢量 c++ 的堆栈结构

Building template Stack structure with vector c++

本文关键字:c++ 堆栈 结构 建模 构建      更新时间:2023-10-16

我在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方法和构造函数。