pomoc z wkładką na pierwszym BST

głosy
1

EDIT tam mała rzecz, która mi brakuje !! błąd jest nadal istnieje

Więc ja próbuje nauczyć się kodować swój pierwszy BST, i trudno .... mam już problemów z zaledwie kilku linii kodu. problem jest we wkładce, ale mam włączone wszystko, abym mógł uzyskać informację zwrotną na temat mojego stylu / inne błędy. Byłem zalecane, aby użyć wskaźnika do realizacji wskaźnika, ale nie miałaś dowiedział się jeszcze, więc nie czujesz komfort / wie jak zakodować go jeszcze.

błąd jest

[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

plik 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

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

movieList.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;
}
Utwórz 27/04/2011 o 03:15
źródło użytkownik
W innych językach...                            


4 odpowiedzi

głosy
2

Test drzewa (); Nie jest to, jak zdefiniować obiekt klasy test, to acutally zadeklarować funkcję o nazwie test, który zwraca Tree.

próbować

Test drzewo; test.instert (100 "bla"); return 0;

Odpowiedział 27/04/2011 o 03:34
źródło użytkownik

głosy
1

Kilka punktów:

Musisz zmienić nazwę zmiennej członek korzenia do czegoś indziej Polecam m_root lub my_root lub tree_root, czy coś z tych rodzajów. Teraz masz trochę starcia przestrzeni nazw w dowolnej funkcji w którym zawierać pierwiastek jako argument. To pozwala także śledzić który korzeń masz na myśli.

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

Trzeba zmienić korzeń na komentowanej linii pierwiastkowania kwadratowego> Key.

Poza tym, wygląda na to, że będzie działać.

EDIT: Również, co powiedział drugi facet. Deklarujesz obiekt jako

TYPE name ()

jeśli wywołanie konstruktora domyślnego (), dzięki czemu kod w swojej głównej funkcji powinny być

Tree test;
test.insert(...)
Odpowiedział 27/04/2011 o 03:38
źródło użytkownik

głosy
1

I kopiowane niektóre kodu i to działa dobrze dla mnie:

Główny:

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

    int main()
    {
      Tree test;
      test.insert(100, "blah");
      test.insert(50, "fifty");
      test.insert(110, "one hundred ten");
      return 0;
    }

funkcję wstawić:

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

Inne niż, że masz błędy składniowe w każdym miejscu. Ja również zmienił nazwę, ponieważ jak ktoś wskazał było trochę problem nazewnictwa. CurrentRoot sens, ponieważ jesteś przekazując jej korzeń lewym lub prawym poddrzewie na każdym rekursji.

Odpowiedział 27/04/2011 o 03:39
źródło użytkownik

głosy
0

Nie należy dodawać tree.cppdo polecenia kompilacji?

[Trinhc @ CS1 Assignment_3] $ g ++ -o a.out movieList.cpp

Stanie się

[Trinhc @ CS1 Assignment_3] $ g ++ -o a.out tree.cpp movieList.cpp

Odpowiedział 27/04/2011 o 05:01
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more