segíteni betét első BST

szavazat
1

EDIT van egy kis dolog, hogy én vagyok a hiányzó !! A hiba továbbra is fennáll

Tehát én próbál tanulni, hogyan kell kódolni az első BST, és ez nehéz .... én már gondjai csak néhány sornyi kódot. A probléma az, hogy a betét, de én mindent beletettünk, hogy tudtam egy kis visszajelzést az én stílusom / egyéb hibákat. Azt javasolták, hogy egy mutató pointer végrehajtására, de mi kikötõ tanultam meg, úgyhogy nem érzi kényelem / tudja, hogyan kell a kódot, hogy még. a

hiba

[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

A tree.h fájl

#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;
}
A kérdést 27/04/2011 03:15
a forrás felhasználó
Más nyelveken...                            


4 válasz

szavazat
2

Fa teszt (); nem, hogyan határozza meg a tárgy osztály Test, ez acutally nyilvánítja nevű függvény vizsgálat, amely visszaadja fa.

próbáld ki

Fa teszt; test.instert (100, "bla"); vissza 0;

Válaszolt 27/04/2011 03:34
a forrás felhasználó

szavazat
1

Pár pontot:

Meg kell változtatni a nevét, a tagok változó gyökér valami máshol én ajánlom m_root vagy my_root vagy tree_root, vagy valami e fajta. Most van egy kicsit a névtér összecsapás minden funkció, amelyen szerepel gyökér érvként. Ez is engedi, hogy melyik gyökér te hivatkozva.

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

Meg kell változtatni a root a hozzászólást vonal gyökér-> billentyűt.

Más, mint, hogy úgy néz ki, hogy jó lesz.

EDIT: Is, amit a másik srác azt mondta. Ön kijelenti egy objektumot

TYPE name ()

ha hív az alapértelmezett konstruktor (), így a kódot a fő funkciója legyen

Tree test;
test.insert(...)
Válaszolt 27/04/2011 03:38
a forrás felhasználó

szavazat
1

Másoltam néhány kódot, és ez jól működik nekem:

fő:

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

Beszúrása funkció:

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

Más, mint, hogy van szintaktikai hibák az egész hely. Azt is megváltoztatta a nevét, mert valaki rámutatott volt egy kis elnevezési probléma. CurrentRoot értelme, mert akkor halad a gyökér a bal vagy a jobb részfa minden rekurziót.

Válaszolt 27/04/2011 03:39
a forrás felhasználó

szavazat
0

Nem kellene hozzá tree.cppaz építmény parancs?

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

válna

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

Válaszolt 27/04/2011 05:01
a forrás felhasználó

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