簡體   English   中英

使用矢量C ++構建模板堆棧結構

[英]Building template Stack structure with vector c++

我正在C ++中基於矢量實現模板化的Stack結構。 我不確定我的代碼有什么問題。

堆棧

#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_ */

Stack.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() 彼此的構造函數,運算符和方法也是如此。 將模板聲明添加到每個實現中以解決此錯誤。 請注意,錯誤消息通過說“無效使用沒有參數列表的模板名稱'Stack'來提示錯誤。

范例:

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方法和構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM