c++ - 返回新的 BinarySearchTree - 缺少类模板的参数列表

标签 c++ templates recursion binary-search-tree

我们的任务是创建一个具有一些基本功能的二叉搜索树。我觉得如果不是因为作业中包含的文件,我们需要遵守这些文件,以便评分者使用他们的评分程序实现我们的代码,我觉得我能够通过。给学生一个名为“Factory.cpp”的文件,该文件具有尝试返回“BinarySearchTree”对象的函数(return new BinarySearchTree();)。但是,VS 2013 给了我标题中看到的错误。经过一些研究,我找不到任何可以实现到我自己的问题中以消除错误的信息。模板类显然更抽象,我找不到要包含/省略的内容等以使事情正常进行。

以下是我目前在 BinarySearchTree.h 中的不完整代码:

#pragma once
#include "BSTInterface.h"
#include "NodeInterface.h"

#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H

    struct BTNode :public NodeInterface{
        // Data Fields
        int data;
        BTNode* left;
        BTNode* right;

        // Constructor
        BTNode(const int& the_data,
            BTNode* left_val = NULL,
            BTNode* right_val = NULL) :
            data(the_data), left(left_val), right(right_val) {}

        // Destructor (to avoid warning message)
        virtual ~BTNode() {}

        // Interface Functions
        int getData(){
            return data;
        }

        NodeInterface* getLeftChild(){
            return left;
        }

        NodeInterface* getRightChild(){
            return right;
        }

    }; // End BTNode



#include <sstream>

template<class T>
class BinarySearchTree:public BSTInterface
{

public:
    BTNode* root;
    // BST Constructor / Deconstructor
    BinarySearchTree() : root(NULL){}

    //BinarySearchTree(const int& the_data,
    //  const BinarySearchTree& left_child = BinarySearchTree(),
    //  const BinarySearchTree& right_child = BinarySearchTree()) :
    //  root(new BTNode(the_data, left_child.root, right_child.root)){}

    virtual ~BinarySearchTree(){}

    // Interface Functions ----------------------

    NodeInterface* getRootNode(){
        return root;
    }


    bool add(int data){
        return addRec(root, data);
    }


    bool addRec(BTNode* &x, int data){
        if (x == NULL){
            if (Search(root, data) == true){
                return false;
            }
            else{
                root = GetNewNode(data);
                return true;
            }
        }
        if (data == x->data){
            return false;
        }
        if (x != NULL){
            if (data < x->data){
                return addRec(x->left, data);
            }
            if (data > x->data){
                return addRec(x->right, data);
            }
        }
    }


    bool remove(int data){
        return false;
    }


    bool removeRec(BTNode* &x, int data){
        return false;
    }


    void clear(){

    }
    // ------------------------------------------


    // My Functions -----------------------------

    BTNode* GetNewNode(int data){
        BTNode* newNode = new BTNode();
        newNode->data = data;
        newNode->left = newNode->right = NULL;
        return newNode;
    }

    bool Search(BTNode* root, int data) {
        if (root == NULL) {
            return false;
        }
        else if (root->data == data) {
            return true;
        }
        else if (data < root->data) { // had <= instead
            return Search(root->left, data);
        }
        else if (data > root->data) { // had no "if"
            return Search(root->right, data);
        }
    }
    // ------------------------------------------
};
#endif

它源自以下 2 个“接口(interface)”文件:

节点接口(interface).h:

//YOU MAY NOT MODIFY THIS DOCUMENT    
#pragma once    
#include <iostream>    
class NodeInterface {
public:
    NodeInterface() {}
    virtual ~NodeInterface() {}

    /*Returns the data that is stored in this node*/
    virtual int getData() = 0;

    /*Returns the left child of this node or null if it doesn't have one.*/
    virtual NodeInterface * getLeftChild() = 0;

    /*Returns the right child of this node or null if it doesn't have one.*/
    virtual NodeInterface * getRightChild() = 0;

};

BSTInterface.h

//YOU MAY NOT MODIFY THIS DOCUMENT    
#pragma once    
#include "NodeInterface.h"  
using namespace std;    
class BSTInterface {
public:
    BSTInterface() {}
    virtual ~BSTInterface() {}

    //Please note that the class that implements this interface must be made
    //of objects which implement the NodeInterface

    /*Returns the root node for this tree*/
    virtual NodeInterface * getRootNode() = 0;

    /*Attempts to add the given int to the BST tree*/
    virtual bool add(int data) = 0;

    /*Attempts to remove the given int from the BST tree*/
    virtual bool remove(int data) = 0;

    /*Removes all nodes from the tree, resulting in an empty tree.*/
    virtual void clear() = 0;
};

然后他们给了我们“Factory.h”和“Factory.cpp”,我相信他们用它们来获取我们的 BinarySearchTree,以便使用他们的评分程序进行评分:

工厂.h:

    #include "BSTInterface.h"
    using namespace std;
    /*
    WARNING: It is expressly forbidden to modify any part of this document, including its name
    */

    class Factory
    {
    public:
        static BSTInterface * getBST();
    };     

工厂.cpp:

#include "Factory.h"
#include "BinarySearchTree.h"
//You may add #include statements here

/*
    You will MODIFY THIS DOCUMENT.

    getBST()
    Creates and returns an object whose class extends BSTInterface.
    This should be an object of a class you have created.
    Example: If you made a class called "BinarySearchTree", you might say, "return new BinarySearchTree();".
*/
BSTInterface * Factory::getBST()
{
    return new BinarySearchTree();//Modify this line
}

在“Factory.cpp”中,BinarySearchTree 在 VS 中被标记为错误,消息为“类模板的参数列表丢失”。我该如何解决?以及您看到的任何其他错误。

另外,我如何在 main() 中声明一个新的 BinarySearchTree 对象并调用它的函数来测试它?

最佳答案

对于该错误,在这些行中:

template<class T>
class BinarySearchTree:public BSTInterface
{

去掉第一行。该行告诉编译器您的 BinarySearchTree 类是模板类。但是由于您的类使用 int 作为数据,因此似乎不需要。

我没有看过您的其他代码,所以我不会对其他任何内容发表评论。

关于c++ - 返回新的 BinarySearchTree - 缺少类模板的参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33726829/

相关文章:

c# - 为 CvMat.data union 创建 C# 结构或以任何方式在 C# 中访问它

rapply 到 R 中数据框的嵌套列表

Scala:在元素列表的元素之间递归搜索

php - 多维数组的递归

c++ - Xpost 在 C++ 中使用 libcurl

c++ - 在 C++ 中使用带有 sprintf() 的格式字符串的警告

c++ - 类体之外的成员函数定义导致错误

c++ - 给定 std::tuple<T...> 时可变参数模板类的实例化?

c++ - 如果用作模板参数的类型在需要完整类型的上下文中内部使用,则何时必须完整?

C++根据模板参数值更改成员函数定义