为什么我的类函数在main中被 "未定义",我是不是在代码中遗漏了什么?

类中的函数本身工作正常,错误似乎有点奇怪,我不明白问题出在哪里.Class Header/too long。

#include <iostream>
using namespace std;
struct Node
{
    int value;
    Node* left;
    Node* right;
};
class BinarySearchTree
{
private:
    int isIdentical;
    int sum;
    int sum1;
    int sum2;
public:
    bool checkBSTidentical(Node* x, Node* y);
    int BSTidentical(Node* firstTree, Node* secondTree);
    int BSTnotIdentical(Node* firstTree, Node* secondTree);
    int BSTsum(Node* x);
    int compareBST(Node* root1, Node* root2);
    Node* build(int data);
    Node* insert(Node* node, int Value);
    void in(Node* root);
};

CPP类/太长

主/所有的类函数都没有定义,虽然我声明了类

#include <iostream>
#include "BinarySearchTree.h"
using namespace std;
int main()
{
    Node* tree1 = NULL;
    Node* tree2 = NULL;
    int x;
    tree1 = insert(tree1, 50); 
    tree1 = insert(tree1, 40);
    tree1 = insert(tree1, 30);
    tree1 = insert(tree1, 20);
    tree1 = insert(tree1, 10);
    cout << "Binary Tree 1: " << endl;
    in(tree1);
    cout << endl;
    tree2 = insert(tree2, 50);
    tree2 = insert(tree2, 40);
    tree2 = insert(tree2, 30);
    tree2 = insert(tree2, 20);
    tree2 = insert(tree2, 10);
    cout << "Binary Tree 2: " << endl;
    in(tree2);
    cout << endl;
    x = compareBST(tree1, tree2);
    cout << "Result: " << x << endl;
    return 0;
}

解决方案:

insert(), in(), compareBST()的非静态方法,这些都是 BinarySearchTree 类,但你 main() 试图把它们当作独立的函数来调用。这就是为什么你会得到 “找不到标识符 “的错误。你需要在一个对象的 BinarySearchTree例如:

#include <iostream>
#include "BinarySearchTree.h"
using namespace std;

int main()
{
    BinarySearchTree bsTree;
    Node* tree1 = NULL;
    Node* tree2 = NULL;
    int x;
    tree1 = bsTree.insert(tree1, 50); 
    tree1 = bsTree.insert(tree1, 40);
    tree1 = bsTree.insert(tree1, 30);
    tree1 = bsTree.insert(tree1, 20);
    tree1 = bsTree.insert(tree1, 10);
    cout << "Binary Tree 1: " << endl;
    bsTree.in(tree1);
    cout << endl;
    tree2 = bsTree.insert(tree2, 50);
    tree2 = bsTree.insert(tree2, 40);
    tree2 = bsTree.insert(tree2, 30);
    tree2 = bsTree.insert(tree2, 20);
    tree2 = bsTree.insert(tree2, 10);
    cout << "Binary Tree 2: " << endl;
    bsTree.in(tree2);
    cout << endl;
    x = bsTree.compareBST(tree1, tree2);
    cout << "Result: " << x << endl;
    return 0;
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

从Firebase实时数据库中随机查询特定用户ID(Flutter)

2022-9-10 4:46:39

未分类

h3标签输出2行,且Font Awesome图标不显示。

2022-9-10 4:46:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索