C ++二叉树错误:(Y)中成员(X)的请求是非类型(Z)

时间:2010-01-30 04:07:22

标签: c++ binary-tree

嘿所有,所以我正在尝试构建一个简单的二叉树,它有两个键并评估其排序的总和。这是它的样子:

struct SumNode
{
    int keyA;
    int keyB;
    SumNode *left;
    SumNode *right;
};

class SumBTree
{
    public:
        SumBTree();
        ~SumBTree();

        void insert(int, int);
        SumNode *search(int, int);
        SumNode *search(int);
        void destroy_tree();

    private:
        SumNode *root;

        void insert(int,int, SumNode*);
        SumNode *search(int,int, SumNode*);
        SumNode *search(int, SumNode*);
        void destroy_tree(SumNode*);
};


SumBTree::SumBTree()
{
    root = NULL;
}

SumBTree::~SumBTree(){};


void SumBTree::insert(int a, int b, SumNode *leaf)
{
    int sum = a + b;
    int leafsum = leaf->keyA + leaf->keyB;

    if (sum < leafsum)
    {
        if (leaf->left != NULL)
        {
            insert(a,b, leaf->left);
        }
        else
        {
            leaf->left = new SumNode;
            leaf->left->keyA = a;
            leaf->left->keyB = b;
            leaf->left->left = NULL;
            leaf->left->right = NULL;
        }
    }
    else
    {
        if (leaf -> right != NULL)
        {
            insert(a,b, leaf->right);
        }
        else
        {
            leaf->right = new SumNode;
            leaf->right->keyA = a;
            leaf->right->keyB = b;
            leaf->right->left = NULL;
            leaf->right->right = NULL;
        }
    }
}

SumNode *SumBTree::search(int a, int b, SumNode *leaf)
{
    if (leaf != NULL)
    {
         if (a == leaf->keyA && b == leaf->keyB)
            return leaf;

        int sum = a + b;
        int leafsum = leaf->keyA + leaf->keyB;

        if (sum < leafsum)
            return search(a, b, leaf->left);

        return search(a, b, leaf->right);


    }
    return NULL;
}


SumNode *SumBTree::search(int sum, SumNode *leaf)
{
    if (leaf != NULL)
    {
        int leafsum = leaf->keyA + leaf->keyB;

        if (sum == leafsum)
            return leaf;

        if (sum < leafsum)
            return search(sum, leaf->left);

        return search(sum, leaf->right);


    }
    return NULL;
}

void SumBTree::destroy_tree(SumNode *leaf)
{
    if(leaf != NULL)
    {
        destroy_tree(leaf->left);
        destroy_tree(leaf->right);
        delete leaf;
    }
}


void SumBTree::insert(int a, int b)
{
    if (root != NULL)
    {
        insert(a,b, root);
    }
    else
    {
        root = new SumNode;
        root->keyA = a;
        root->keyB = b;
        root->left = NULL;
        root->right = NULL;
    }
}

SumNode *SumBTree::search(int a, int b)
{
    return search(a, b, root);
}

SumNode *SumBTree::search(int sum)
{
    return search(sum, root);
}

void SumBTree::destroy_tree()
{
    destroy_tree(root);
}

我用它测试了它:

#include <iostream>
#include "SumBTree.h"

using namespace std;

int main()
{
    cout << "Initializing Tree" << endl;
    SumBTree sbt();

    cout << "Inserting (2,3)" << endl;
    sbt.insert(2,3);



    cout << "Hello world!" << endl;
    return 0;
}

但每当我尝试构建它时,我都会收到以下错误:

|| === BTree,Debug === | C:\ Users \ Axel \ Desktop \ coding \ C ++ Projects \ BTree \ main.cpp ||函数int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for member插入'sbt', which is of non-class type SumBTree()()'| || ===构建完成:1个错误,0个警告=== |

我无法弄清楚我做错了什么!它是重载函数吗?我不明白。 这里有人知道吗?

1 个答案:

答案 0 :(得分:5)

你需要替换SumBTree sbt();与SumBTree sbt;

现在它认为sbt不是一个类。如果构造函数中没有参数,请不要使用空括号。