Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么<;错误类型>;从模板类切换时出现?_C++_Compiler Errors_Binary Search Tree - Fatal编程技术网

C++ 为什么<;错误类型>;从模板类切换时出现?

C++ 为什么<;错误类型>;从模板类切换时出现?,c++,compiler-errors,binary-search-tree,C++,Compiler Errors,Binary Search Tree,我使用模板类实现了以下二进制搜索树: #ifndef ROSTER_ #define ROSTER_ #include "BinaryNode.hpp" #include "Visitor.hpp" #include "Student.hpp" #include "Printer.hpp" #include <vector> #include <memory> template<class T> class Roster { public: Ros

我使用模板类实现了以下二进制搜索树:

#ifndef ROSTER_
#define ROSTER_

#include "BinaryNode.hpp"
#include "Visitor.hpp"
#include "Student.hpp"
#include "Printer.hpp"
#include <vector>
#include <memory>

template<class T>
class Roster
{
public:
    Roster(); // Constructor
    Roster(const Roster<T>& tree); //Copy constructor
    ~Roster(); // Destructor
    bool isEmpty() const;
    int getHeight() const;
    int getNumberOfNodes() const;
    void add(const T& new_item);
    void add(const std::vector<T>& new_items);
    void remove(const T& target);
    void display();
    T find(const T& item);
    void clear();

    void inorderTraverse(Visitor<T>& visit) const;

    Roster& operator= (const Roster<T>& rhs);

private:
    std::shared_ptr<BinaryNode<T>> root_ptr_;
    std::shared_ptr<BinaryNode<T>> copyTree(const std::shared_ptr<BinaryNode<T>> old_tree_root_ptr) const;
    void destroyTree(std::shared_ptr<BinaryNode<T>> sub_tree_ptr);
    int getHeightHelper(std::shared_ptr<BinaryNode<T>> sub_tree_ptr) const;
    auto placeNode(std::shared_ptr<BinaryNode<T>> sub_tree_ptr, std::shared_ptr<BinaryNode<T>> new_node_ptr);
    auto removeValue(std::shared_ptr<BinaryNode<T>> sub_tree_ptr, const T target);
    auto removeNode(std::shared_ptr<BinaryNode<T>> node_ptr);
    auto removeLeftmostNode(std::shared_ptr<BinaryNode<T>> nodePtr, T& inorderSuccessor);
    void inorder(Visitor<T>& visit, std::shared_ptr<BinaryNode<T>> tree_ptr) const;
    //Operator overloading for students objects
    friend bool operator <(const Student& a, const Student& b);
    friend bool operator >(const Student& a, const Student& b);
    friend bool operator ==(const Student& a, const Student& b);
};
#include "Roster.cpp"
#endif
#ifndef名册_
#确定名册_
#包括“BinaryNode.hpp”
#包括“Visitor.hpp”
#包括“Student.hpp”
#包括“Printer.hpp”
#包括
#包括
模板
班级花名册
{
公众:
花名册();//构造函数
花名册(const花名册和树);//复制构造函数
~floster();//析构函数
bool isEmpty()常量;
int getHeight()常量;
int getNumberOfNodes()常量;
无效添加(const T&new_项目);
无效添加(const std::vector和new_项);
无效清除(常数T和目标);
void display();
T查找(const T&item);
无效清除();
OrderTraverse(访客和访问)常数中的void;
排班和操作员=(施工排班和rhs);
私人:
std::共享根目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录目录;
std::shared_ptr copyTree(const std::shared_ptr old_tree_root_ptr)const;
void destroy树(std::shared_ptr sub_ptr_ptr);
int getHeightHelper(std::shared_ptr sub_ptr)const;
自动放置节点(std::shared_ptr子树_ptr,std::shared_ptr new_ptr);
自动删除值(std::shared_ptr sub_ptr_ptr,const T target);
自动删除节点(std::shared_ptr node_ptr);
自动移除主节点(std::shared_ptr nodePtr、T&inorderSuccessor);
无效索引(访客和访问,标准::共享目录树)常量;
//对象的运算符重载
friend bool操作员(const Student&a、const Student&b);
friend bool运算符==(常量学生&a、常量学生&b);
};
#包括“花名册.cpp”
#恩迪夫
在这种情况下,我希望实现相同的类,但只使用Student类型的对象。当声明为<代码>花名册my_student时,实现工作

但是,当我尝试实现相同的代码,但不是一个模板类,而是一个只接受student类型对象的类,因此删除模板并为BinaryNode指定每个函数的数据类型时,我在两个特定函数上得到一个错误时间:

auto removeNode(std::shared_ptr<BinaryNode<Student>> node_ptr);
auto removeLeftmostNode(std::shared_ptr<BinaryNode<Student>> nodePtr, Student& inorderSuccessor);
auto-removeNode(std::shared_ptr node_ptr);
自动移除主节点(std::shared_ptr nodePtr、Student和inorderSuccessor);
我收到一条“声明与”消息:

auto Roster::removeNode(std::shared_ptr<BinaryNode<Student>> node_ptr)
{

}
自动排班::removeNode(标准::共享\u ptr节点\u ptr)
{
}
要查看模板类的.cpp for花名册.hpp的完整实现,请执行以下操作:

#include "Roster.hpp"

//Constructor
template<class T>
Roster<T>::Roster()
{
    root_ptr_ = nullptr;
}

//Copy Constructor
template<class T>
Roster<T>::Roster(const Roster& tree)
{
    root_ptr_ = copyTree(tree.root_ptr_);
}

//Destructor
template<class T>
Roster<T>::~Roster()
{
    destroyTree(root_ptr_);
}

//Check if empty
template<class T>
bool Roster<T>::isEmpty() const
{
    return root_ptr_ == nullptr;
}

//Clear
template<class T>
void Roster<T>::clear()
{
    destroyTree(root_ptr_);
}

//Get height
template<class T>
int Roster<T>::getHeight() const
{
    return getHeightHelper(root_ptr_);
}

//Add
template<class T>
void Roster<T>::add(const T& new_item)
{
    auto new_node_ptr = std::make_shared<BinaryNode<T>>(new_item);
    root_ptr_ = placeNode(root_ptr_, new_node_ptr);
}

//Add with vector
template<class T>
void Roster<T>::add(const std::vector<T>& new_items)
{
    for(int i = 0; i < new_items.size(); i++)
    {
        auto new_node_ptr = std::make_shared<BinaryNode<T>>(new_items[i]);
        root_ptr_ = placeNode(root_ptr_, new_node_ptr);
    }
}

//Remove
template<class T>
void Roster<T>::remove(const T& target)
{
    root_ptr_ = removeValue(root_ptr_, target);
}

//Diplay
template<class T>
void Roster<T>::display()
{
    Printer p;
    inorderTraverse(p);
}

template<class T>
void Roster<T>::inorderTraverse(Visitor<T>& visit) const
{
    inorder(visit, root_ptr_);
}

//Helper function
template<class T>
std::shared_ptr<BinaryNode<T>> Roster<T>::copyTree(const std::shared_ptr<BinaryNode<T>> old_tree_root_ptr) const
{
    std::shared_ptr<BinaryNode<T>> new_tree_ptr;

    // Copy tree nodes during preorder tranversal
    if(old_tree_root_ptr != nullptr)
    {
        new_tree_ptr = std::make_shared<BinaryNode<T>>(old_tree_root_ptr->getItem(), nullptr, nullptr);
        new_tree_ptr->setLeftChildPtr(copyTree(old_tree_root_ptr->getLeftChildPtr()));
        new_tree_ptr->setRightChildPtr(copyTree(old_tree_root_ptr->getRightChildPtr()));
    }
    return new_tree_ptr;
}
template<class T>
void Roster<T>::destroyTree(std::shared_ptr<BinaryNode<T>> sub_tree_ptr)
{
    if(sub_tree_ptr != nullptr)
    {
        destroyTree(sub_tree_ptr->getLeftChildPtr());
        destroyTree(sub_tree_ptr->getRightChildPtr());
        sub_tree_ptr.reset();
    }
}

template<class T>
int Roster<T>::getHeightHelper(std::shared_ptr<BinaryNode<T>> sub_tree_ptr) const
{
    if(sub_tree_ptr == nullptr)
    {
        return 0;
    }
    else
    {
        return 1 + std::max(getHeightHelper(sub_tree_ptr->getLeftChildPtr()), getHeightHelper(sub_tree_ptr->getRightChildPtr()));
    }
}

template<class T>
auto Roster<T>::placeNode(std::shared_ptr<BinaryNode<T>> sub_tree_ptr, std::shared_ptr<BinaryNode<T>> new_node_ptr)
{
    if(sub_tree_ptr == nullptr)
    {
        return new_node_ptr;
    }
    else
    {
        if(sub_tree_ptr->getItem() > new_node_ptr->getItem())
        {
            sub_tree_ptr->setLeftChildPtr(placeNode(sub_tree_ptr->getLeftChildPtr(), new_node_ptr));
        }
        else
        {
            sub_tree_ptr->setRightChildPtr(placeNode(sub_tree_ptr->getRightChildPtr(), new_node_ptr));
        }
        return sub_tree_ptr;
    }
}

template<class T>
auto Roster<T>::removeValue(std::shared_ptr<BinaryNode<T>> sub_tree_ptr, const T target)
{
    if(sub_tree_ptr == nullptr)
    {
        return sub_tree_ptr;
    }
    if(sub_tree_ptr->getItem() == target)
    {
        sub_tree_ptr = removeNode(sub_tree_ptr);
        return sub_tree_ptr;
    }
    else
    {
        if(sub_tree_ptr->getItem() > target)
        {
            sub_tree_ptr->setLeftChildPtr(removeValue(sub_tree_ptr->getLeftChildPtr(), target));
        }
        else
        {
            sub_tree_ptr->setRightChildPtr(removeValue(sub_tree_ptr->getRightChildPtr(), target));
        }
        return sub_tree_ptr;
    }
}

template<class T>
auto Roster<T>::removeNode(std::shared_ptr<BinaryNode<T>> node_ptr)
{
    //Case 1: Node is a leaf - it is deleted
    if(node_ptr->isLeaf())
    {
        node_ptr.reset();
        return node_ptr;
    }
    //Case 2: Node has one child - parent adopts child
    else if(node_ptr->getLeftChildPtr() == nullptr)
    {
        return node_ptr->getRightChildPtr();
    }
    else if(node_ptr->getRightChildPtr() == nullptr)
    {
        return node_ptr->getLeftChildPtr();
    }
    else
    {
        T new_node_value;
        node_ptr->setRightChildPtr(removeLeftmostNode(node_ptr->getRightChildPtr(), new_node_value));

        node_ptr->setItem(new_node_value);
        return node_ptr;
    }
}

template<class T>
auto Roster<T>::removeLeftmostNode(std::shared_ptr<BinaryNode<T>> nodePtr, T& inorderSuccessor)
{
    if(nodePtr->getLeftChildPtr() == nullptr)
    {
        inorderSuccessor = nodePtr->getItem();
        return removeNode(nodePtr);
    }
    else
    {
        nodePtr->setLeftChildPtr(removeLeftmostNode(nodePtr->getLeftChildPtr(), inorderSuccessor));
        return nodePtr;
    }
}

template<class T>
void Roster<T>::inorder(Visitor<T>& visit, std::shared_ptr<BinaryNode<T>> tree_ptr) const
{
    if(tree_ptr != nullptr)
    {
        inorder(visit, tree_ptr->getLeftChildPtr());
        T the_item = tree_ptr->getItem();
        visit(the_item);
        inorder(visit, tree_ptr->getRightChildPtr());
    }
}

//Operator Overloading for students objects
bool operator <(const Student& a, const Student& b)
{
    if( a.getLastName() < b.getLastName())
    {
        return true;
    }
    else if(a.getLastName() == b.getLastName())
    {
        if(a.getFirstName() < b.getFirstName())
        {
            return true;
        }
    }
    return false;
}

bool operator >(const Student& a, const Student& b)
{
    if( a.getLastName() > b.getLastName())
    {
        return true;
    }
    else if(a.getLastName() == b.getLastName())
    {
        if(a.getFirstName() > b.getFirstName())
        {
            return true;
        }
    }
    return false;
}

bool operator ==(const Student& a, const Student& b)
{
    return (a.getFirstName() == b.getFirstName() && a.getLastName() == b.getLastName());
}
#包括“花名册.hpp”
//建造师
模板
花名册::花名册()
{
root_ptr_uu=nullptr;
}
//复制构造函数
模板
花名册::花名册(const花名册和树)
{
root\u ptr\u=复制树(tree.root\u ptr\u);
}
//析构函数
模板
花名册::~花名册()
{
销毁树(根目录);
}
//检查是否为空
模板
bool花名册::isEmpty()常量
{
返回root_ptr_u==nullptr;
}
//清楚的
模板
作废花名册::清除()
{
销毁树(根目录);
}
//获得高度
模板
int花名册::getHeight()常量
{
返回getHeightHelper(root_ptr_);
}
//加
模板
作废花名册::添加(常量T和新项目)
{
自动新建_节点_ptr=std::使_共享(新建_项);
根节点=放置节点(根节点、新节点);
}
//加向量
模板
作废花名册::添加(常量标准::向量和新项目)
{
对于(int i=0;igetItem(),nullptr,nullptr);
新建_-tree_-ptr->setLeftChildPtr(copyTree(旧_-tree_-root_-ptr->getLeftChildPtr());
新建_-tree_-ptr->setRightChildPtr(copyTree(旧_-tree_-root_-ptr->getRightChildPtr());
}
返回新的_树_ptr;
}
模板
作废花名册::销毁目录树(标准::共享目录子目录树)
{
if(子树ptr!=nullptr)
{
destroyTree(sub_tree_ptr->getLeftChildPtr());
destroyTree(sub_tree_ptr->getRightChildPtr());
子_树_ptr.reset();
}
}
模板
int花名册::getHeightHelper(标准::共享子树)常量
{
if(子_树_ptr==nullptr)
{
返回0;
}
其他的
{
返回1+std::max(getHeightHelper(sub_tree_ptr->getLeftChildPtr()),getHeightHelper(sub_tree_ptr->getRightChildPtr());
}
}
模板
自动花名册::placeNode(标准::共享目录子目录树,标准::共享目录新目录)
{
if(子_树_ptr==nullptr)
{
返回新的_节点_ptr;
}
其他的
{
if(子树\u ptr->getItem()>新节点\u ptr->getItem())
{
子_树_ptr->setLeftChildPtr(placeNode(子_树_ptr->getLeftChildPtr(),新_节点_ptr));
}
其他的
{
子树\u ptr->setRightChildPtr(placeNode(子树\u ptr->getRightChildPtr(),新的\u节点\u ptr));
}
返回子_树_ptr;
}
}
模板
自动花名册::删除值(标准::共享目录子目录树目录,常数目标)
{
if(子_树_ptr==nullptr)
{
返回子_树_ptr;
}
if(子树\u ptr->getItem()==目标)
{
sub_tree_ptr=移除节点(sub_tree_ptr);
返回子_树_ptr;
}
其他的
{
if(子树->getItem()>目标)
{
sub_tree_ptr->setLeftChildPtr(移除值(sub_tree_ptr->getLeftChildPtr(),目标));
}
其他的
{
sub_tree_ptr->setRightChildPtr(移除值(sub_tree_ptr->getRightChildPtr(),目标));
}
返回子_树_ptr;
}
}
模板
自动花名册::删除节点(标准::共享节点)
{
//案例1:节点是叶-已删除
if(node_ptr->isLeaf())
{
#ifndef ROSTER_HPP
#define ROSTER_HPP

#include "BinaryNode.hpp"
#include "Visitor.hpp"
#include "Student.hpp"
#include "Printer.hpp"
#include <vector>
#include <memory>

class Roster
{
public:
    Roster(); // Constructor
    Roster(const Roster& tree); //Copy constructor
    ~Roster(); // Destructor
    bool isEmpty() const;
    int getHeight() const;
    int getNumberOfNodes() const;
    void add(const Student& new_item);
    void add(const std::vector<Student>& new_items);
    void remove(const Student& target);
    void display();
    Student find(const Student& item);
    void clear();

    void inorderTraverse(Visitor<Student>& visit) const;

    Roster& operator= (const Roster& rhs);

private:
    std::shared_ptr<BinaryNode<Student>> root_ptr_;
    std::shared_ptr<BinaryNode<Student>> copyTree(const std::shared_ptr<BinaryNode<Student>> old_tree_root_ptr) const;
    void destroyTree(std::shared_ptr<BinaryNode<Student>> sub_tree_ptr);
    int getHeightHelper(std::shared_ptr<BinaryNode<Student>> sub_tree_ptr) const;
    auto placeNode(std::shared_ptr<BinaryNode<Student>> sub_tree_ptr, std::shared_ptr<BinaryNode<Student>> new_node_ptr);
    auto removeValue(std::shared_ptr<BinaryNode<Student>> sub_tree_ptr, const Student target);
    auto removeNode(std::shared_ptr<BinaryNode<Student>> node_ptr);
    auto removeLeftmostNode(std::shared_ptr<BinaryNode<Student>> nodePtr, Student& inorderSuccessor);
    void inorder(Visitor<Student>& visit, std::shared_ptr<BinaryNode<Student>> tree_ptr) const;
    //Operator overloading for students objects
    friend bool operator <(const Student& a, const Student& b);
    friend bool operator >(const Student& a, const Student& b);
    friend bool operator ==(const Student& a, const Student& b);
};

#endif
declaration is incompatible with <error-type>
Roster::removeNode(std::__1::shared_ptr<BinaryNode<Student>> node_ptr)