C ++二叉树错误:请求(Y)中非类类型(Z)的成员(X) [英] C++ Binary Tree error: request for member (X) in (Y) which is of non-class type (Z)

查看:62
本文介绍了C ++二叉树错误:请求(Y)中非类类型(Z)的成员(X)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,所以我试图构建一个具有两个键的简单二叉树并评估其总和.这是它的样子:

Hey all, so I am trying to build a simple binary tree that has two keys and evaluates the sum for its sorting. Here is what it's looking like:

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);
}

我使用以下方法进行了测试:

I went to test it out, using this:

#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;
}

但是每当我尝试构建它时,都会出现以下错误:

But whenever I try to build it I get the following error:

|| === BTree,调试=== | 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 insert'中sbt', which is of non-class type SumBTree()()'| || ===构建完成:1个错误,0个警告=== |

||=== BTree, Debug ===| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp||In function int main()':| C:\Users\Axel\Desktop\coding\C++ Projects\BTree\main.cpp|12|error: request for memberinsert' in sbt', which is of non-class typeSumBTree ()()'| ||=== Build finished: 1 errors, 0 warnings ===|

我不知道自己在做什么错!是重载的功能吗?我不明白 这里有人知道吗?

I can't figure out what I'm doing wrong! Is it the overloaded functions? I don't get it. Does anyone here know?

推荐答案

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

you need to replace SumBTree sbt(); with SumBTree sbt;

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

Right now it thinks sbt isn't a class. If there are no parameters in a constructor don't use empty brackets.

这篇关于C ++二叉树错误:请求(Y)中非类类型(Z)的成员(X)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆