aiutare con inserto in prima BST

voti
1

EDIT c'è una piccola cosa che mi manca !! l'errore è ancora lì

Così sto cercando di imparare a codice mio primo BST, ed è difficile .... sto già avendo difficoltà con poche righe di codice. il problema è nell'inserto, ma ho incluso tutto in modo che ho potuto ottenere un feedback sul mio stile / altri errori. Mi è stato suggerito di usare un puntatore all'implementazione puntatore, ma noi havent ancora imparato, così io non sento il comfort / sai scrivere codice ancora. il

errore è

[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

il file 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;
}
È pubblicato 27/04/2011 alle 03:15
fonte dall'utente
In altre lingue...                            


4 risposte

voti
2

Test Albero (); Non è come definire un oggetto di prova di classe, questa acutally dichiarare funzione di test di nome che restituisce Albero.

provare

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

Risposto il 27/04/2011 a 03:34
fonte dall'utente

voti
1

Paio di punti:

È necessario modificare il nome del membro principale variabile a qualcosa altrove mi consiglia m_root, o my_root o tree_root, o qualcosa di questo genere. In questo momento hai un po 'di uno scontro namespace in ogni funzione in cui si include radice come argomento. Questo vi permetterà anche di tenere traccia di quali radice ti riferisci.

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

È necessario modificare root sulla riga di commento per root-> chiave.

Oltre a questo, sembra che funzionerà.

EDIT: Inoltre, ciò che ha detto l'altro ragazzo. Si dichiara un oggetto come

TYPE name ()

se si chiama il costruttore di default (), in modo che il codice nel tuo funzione principale dovrebbe essere

Tree test;
test.insert(...)
Risposto il 27/04/2011 a 03:38
fonte dall'utente

voti
1

Ho copiato alcuni di codice e questo sta lavorando bene per me:

principale:

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

Funzione inserire:

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

Diverso da quello che avete errori di sintassi in tutto il luogo. Ho anche cambiato il nome perché, come qualcuno ha sottolineato c'era un po 'un problema di denominazione. CurrentRoot ha senso perché si sta passando è la radice del sottoalbero di sinistra o di destra su ogni ricorsione.

Risposto il 27/04/2011 a 03:39
fonte dall'utente

voti
0

Non dovresti aggiungere tree.cppal vostro comando build?

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

Potrebbe diventare

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

Risposto il 27/04/2011 a 05:01
fonte dall'utente

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