c++ - 帮助插入第一个 BST

标签 c++ g++ binary-tree binary-search-tree

编辑我遗漏的一件小事!!错误仍然存​​在

所以我正在尝试学习如何编写我的第一个 BST,这很难......我已经遇到了仅仅几行代码的问题。问题出在插页中,但我已经包含了所有内容,以便我可以获得有关我的风格/其他错误的一些反馈。有人建议我使用指向指针实现的指针,但我们还没有学会它,所以我还不放心/不知道如何编写它。

错误是

[trinhc@cs1 Assignment_3]$ g++ movieList.cpp -o a.out
/tmp/ccLw6nsv.o: In function `main':
movieList.cpp:(.text+0x7a): undefined reference to `Tree::Tree()'
movieList.cpp:(.text+0xa7): undefined reference to `Tree::insert(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

tree.h 文件

#ifndef TREE_H
#define TREE_H

#include <string>
#include <iostream>
using namespace std;

class Tree
{
 public:
  Tree();
  bool insert(int k, string s);

 private:
  struct Node
  {
    int key;
    string data;
    Node *left;
    Node *right;
  };
  Node* root;
  bool insert(Node*& root, int k, string s);
};

#endif

树.cpp

#include <iostream>
#include "tree.h"
#include <stack>
#include <queue>
#include <string>
using namespace std;

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

bool Tree::insert(int k, string s)
{
  return insert(root, k, s);
}

bool Tree::insert(Node*& current_root, int k, string s)
{
  if(root == NULL){
    current_root = new Node;
    current_root->key = k;
    current_root->data = s;
    current_root->left = NULL;
    current_root->right = NULL;
    return true;
  }
  else if (current_root->key == k)
    return false;
  else if (current_root->key > k)
    insert(current_root->left, k, s);
  else
    insert (current_root->right,k, s);
}

电影列表.cpp

#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "tree.h"


using namespace std;

int main()
{
  Tree test;
  test.insert(100, "blah");
  return 0;
}

最佳答案

树测试();不是如何定义类 Test 的对象,这实际上声明了返回 Tree 的名为 test 的函数。

尝试

树测试; test.insert(100, "废话"); 返回 0;

关于c++ - 帮助插入第一个 BST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798616/

相关文章:

c++ - 这个 min() 函数是如何工作的?

c++ - 不用递归就能找到矩阵的行列式吗?

c++ - 如何保护进程不被杀死?

c++ - 无法解析 cc1plus : error: unrecognized command line option "-std=c++11"

c - 最容易在C中实现在线排序数据结构

c++ - 使用 getline() 输入字符串的问题

c++ - Windows C++ 多字节/单码

c++ - 无法编译 OpenCV 代码 (C++)

haskell - 有没有办法避免在插入时复制二叉树的整个搜索路径?

c - 消失的二叉树