gpt4 book ai didi

c++ - 定义模板化类函数时出错

转载 作者:行者123 更新时间:2023-11-27 23:49:26 24 4
gpt4 key购买 nike

我正在为类进行模板分配,但我的代码在这些特定行中出现错误。 “‘T’不是参数‘T’的有效模板类型参数”

这让我很困惑,因为我以前从未使用过 namespace 。我也没有使用过 .inl。有人可以帮助更正我的代码吗?我在编译器给我错误的地方插入了注释,它们是返回 Node* 的函数。我的代码也没有完成。

BST.hpp

#ifndef BST_HPP
#define BST_HPP

namespace Tree{
template<class T>
class BST {
public:
BST();//default constructor
BST(T rootkey);//constructor with 1 parameter for root key
~BST();//destructor
void insert(T value); //function named insert with 1 parameter(key value to insert)
Node* remove(class Node* troot,T value);//function named remove with 1 parameter (key value to remove)
Node* min(class Node* mini);
private:
class Node {
public:
Node();//default constructor
Node(T k);//constructor with 1 parameter (key value)
~Node(); //destructor
Node* getP(); //parent accessor
Node* getL();//left accessor
Node* getR();//right accessor
parent mutator
left mutator
right mutator
operator <
operator ==
operator !=
operator <<
private:
T key;
Node *parent;
Node *left;
Node *right;
};
Node *root;
void destroy(Node* r);


};



};

#include "BST.inl"
#endif

BST.inl

#include "BST.hpp"
template<class T>
inline Tree::BST<T>::BST(T rootkey)
{
root = new Node(rootkey);
root->setP(0);
}

template<class T>
inline Tree::BST<T>::BST()
{
root = new Node();
}


template<class T>
inline Tree::BST<T>::~BST()
{
delete root;
}

template<class T>
inline void Tree::BST<T>::insert(T value)
{
Node *temp = root;
Node *prev;
do
{
if (value < temp->getK())
{
prev = temp;
temp = temp->getL();
}
else if (value >= temp->getK())
{
prev = temp;
temp = temp->getR();
}
} while (temp != NULL);
temp = new Node(value);
temp->setP(prev);
if (temp->getK() > prev->getK())
{
prev->setR(temp);
}
else
{
prev->setL(temp);
}
}

template<class T>
inline Node * Tree::BST<T>::remove(Node *troot, T value)//ERROR
{
Node *temp;
if (troot == NULL)
{
return troot;
}
else if (value > troot->getK())
{
return remove(troot->getR(), value)
}
else if (value < troot->getK())
{
return remove(troot->getL(), value)
}
else
{
if (troot->getL() == NULL && troot->getR() == NULL)
{
delete troot;
troot = NULL;
return troot;
}
else if (troot->getR() == NULL)
{
temp = troot;
troot = troot->getL();
return troot;
}
else if (troot->getL() == NULL)
{
temp = troot;
troot = troot->getR();
return troot;
}
else
{
temp = min(troot->getR());
troot->setK(temp->getK());
troot->getR() = remove(troot->getR, temp->getK());
}
}
return troot;
}

template<class T>
inline Node * Tree::BST<T>::min(Node * mini)//ERROR
{
if (mini->getL() != NULL)
{
mini = mini->getL();
}
else
return mini;
}

template<class T>
inline Tree::BST<T>::Node::Node()
{
key = 0;
}

template<class T>
inline Tree::BST<T>::Node::Node(T k)
{
key = k;
}

template<class T>
inline Tree::BST<T>::Node::~Node()
{
delete getL();
delete getR();
}

template<class T>
inline Node * Tree::BST<T>::Node::getP()//ERROR
{
return parent;
}

template<class T>
inline Node * Tree::BST<T>::Node::getL()//ERROR
{
return left;
}

最佳答案

在这一行中:

template<class T>
inline Node * Tree::BST<T>::Node::getP() {

没有Node在全局范围内上课供您返回。这是唯一的事Node可以引用到此为止,因为编译器里面还没有BST<T>找到内部的范围 Node .有两种方法可以解决此问题:

  1. 完全符合 Node (自 C++98 起有效):

    template<class T>
    inline Tree::BST<T>::Node * Tree::BST<T>::Node::getP() {

    这完全限定了 Node 的范围是。

  2. 使用尾随返回类型(我个人的偏好,C++11 及更高版本):

    template<class T>
    inline auto Tree::BST<T>::Node::getP() -> Node* {

    这会延迟对 Node 的查找直到它可以在类的范围内开始。

关于c++ - 定义模板化类函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com