返回新的BinarySearchTree-类模板的参数列表丢失

return new BinarySearchTree - argument list for class template is missing

本文关键字:参数 列表 BinarySearchTree- 返回      更新时间:2023-10-16

我们有一项任务,用一些基本函数创建一个二进制搜索树。我觉得,如果不是因为作业中包含的文件,我们需要遵守这些文件,以便评分者在评分程序中实施我们的代码,我就能勉强应付。学生会得到一个名为"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个"接口"文件:

NodeInterface.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;
};

BST接口.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"answers"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();
    };     

Factory.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,因此似乎不需要这样做。

我还没有看过你的其他代码,所以我不会对其他任何事情发表评论。