【问题标题】:Nested template class constructor嵌套模板类构造函数
【发布时间】:2015-07-03 00:19:21
【问题描述】:

我的编译器找不到嵌套类的构造函数的定义。

我的嵌套类Node在中间,构造函数在最后。

错误:

错误 C2244:“CircularDoubleDirectedList::Node::Node”:无法 要将函数定义与现有声明匹配,请参见 'CircularDoubleDirectedList::Node::Node'的声明

定义

'CircularDoubleDirectedList::Node::Node(const T &)'

现有声明

'CircularDoubleDirectedList::Node::Node(const T &)'

代码:

#ifndef CIRCULARDOUBLEDIRECTEDLIST_H
#define CIRCULARDOUBLEDIRECTEDLIST_H

#include "ICircularDoubleDirectedList.h"

template <typename T> class CircularDoubleDirectedList;
template <typename T> class Node;

template <typename T>
class CircularDoubleDirectedList :
    public ICircularDoubleDirectedList<T>{
public:
    //Variabels
    Node<T>* current;
    int nrOfElements;
    direction currentDirection;

    //Functions
    CircularDoubleDirectedList();
    ~CircularDoubleDirectedList();
    void addAtCurrent(const T& element) override;

private:
    template <typename T>
    class Node
    {
    public:
        T data;
        Node<T>* forward;
        Node<T>* backward;

        Node(const T& element);// The constructor
    };

};
template <typename T>
CircularDoubleDirectedList<T>::CircularDoubleDirectedList(){
    this->nrOfElements = 0;
    this->current = nullptr;
    this->currentDirection = FORWARD;
}
template <typename T>
CircularDoubleDirectedList<T>::~CircularDoubleDirectedList(){
    //TODO: Destroy all nodes
}
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
    Node<T>* newNode = new Node<T>(element);
    newNode->data = element;
    if (this->nrOfElements == 0){
        newNode->forward = newNode;
        newNode->backward = newNode;
    }
    else{
        //this->current->forward = newNode;
        //this->current->forward->backward = newNode;
    }
    //this->current = newNode;
}
template <typename T>
CircularDoubleDirectedList<T>::Node<T>::Node(const T& element){
    this->data = element;
}

#endif

【问题讨论】:

  • 我认为问题可能是您转发声明的Node 并在标头的模板特化中使用Node。您可能需要在此处自行导入 Node 的头文件。
  • 为什么 Node 类是模板化的?
  • 如何导入头文件本身? ifndef 不应该停止吗?我会在哪里导入它?我尝试在包含后执行此操作,但没有任何反应。

标签: c++


【解决方案1】:

首先,前向声明的template &lt;typename T&gt; class Node;CircularDoubleDirectedList::Node不一样——前者是全局类模板,后者是嵌套类。

其次,您不需要将CircularDoubleDirectedList::Node 声明为模板(如果这样做,您必须为其使用另一个模板参数名称,而不是T)。但据我了解,对于这种情况,您应该将其设为非模板,因此:

template <typename T>
class CircularDoubleDirectedList :
    public ICircularDoubleDirectedList<T>{
private:
    class Node
    {
    public:
        T data;
        Node* forward;
        Node* backward;

        Node(const T& element);// The constructor
    };
public:
    Node* current;
    //...
};

template <typename T>
CircularDoubleDirectedList<T>::Node::Node(const T& element){
    this->data = element;
}

【讨论】:

  • 好吧,做到了。模板和我是很好的朋友。只是一些错误去:/
【解决方案2】:

您有两个名为Node 的类模板,而实际上您想要一个名为Node 的非模板类。你有前向声明的::Node&lt;T&gt;,你有嵌套的::CircularDoubleDirectedList&lt;T&gt;::Node&lt;U&gt;

如果你真的想要这样,你必须在构造函数定义中添加另一个 template 关键字:

template <typename T>  //because CircularDoubleDirectedList is a template
template <typename U>  //because Node is a template
CircularDoubleDirectedList<T>::Node<U>::Node(const T& element) : data(element)
{}

但是,我看不出有一个理由让Node 成为模板。在CircularDoubleDirectedList&lt;T&gt; 内部,是否要使用类型不是T 的节点?如果没有,请将Node 设为普通的非模板类:

template <typename T>
class CircularDoubleDirectedList :
    public ICircularDoubleDirectedList<T>{
public:
    //Variabels
    Node<T>* current;
    int nrOfElements;
    direction currentDirection;

    //Functions
    CircularDoubleDirectedList();
    ~CircularDoubleDirectedList();
    void addAtCurrent(const T& element) override;

private:
    class Node
    {
    public:
        T data;
        Node* forward;
        Node* backward;

        Node(const T& element);// The constructor
    };
};


template <typename T>
CircularDoubleDirectedList<T>::Node::Node(const T& element) : data(element)
{}

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 2019-06-04
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多