पहले BST पर डालने के साथ मदद

वोट
1

वहाँ संपादित करें एक छोटी सी बात यह है कि मुझे याद आ रही !! त्रुटि अभी भी होती है

तो मैं अपनी पहली 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

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;
}
27/04/2011 को 03:15
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


4 जवाब

वोट
2

ट्री परीक्षण (); कैसे वर्ग टेस्ट का एक उद्देश्य को परिभाषित नहीं है, यह acutally समारोह नामित परीक्षण है जो पेड़ रिटर्न की घोषणा।

प्रयत्न

ट्री परीक्षण; test.instert (100, "blah"); 0 वापसी;

27/04/2011 को 03:34
का स्रोत उपयोगकर्ता

वोट
1

अंकों की युगल:

आप कुछ और मैं m_root, या my_root, या tree_root, या उन तरह की कुछ सलाह देते हैं करने के लिए अपने सदस्य चर जड़ का नाम बदलने की जरूरत है। अभी आप किसी भी समारोह जहां एक तर्क के रूप जड़ शामिल में एक नाम स्थान टकराव का एक छोटा सा मिल गया है। यह भी आप जो जड़ आप की बात कर रहे हैं का ट्रैक रखने देंगे।

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

आप टिप्पणी की लाइन root- करने के लिए> कुंजी पर रूट बदलने की जरूरत है।

उसके अलावा, यह है कि यह काम करेंगे लग रहा है।

संपादित करें: इसके अलावा, क्या अन्य पुरुष कहा। आप के रूप में एक वस्तु की घोषणा

TYPE name ()

यदि आप डिफ़ॉल्ट निर्माता () बुला रहे हैं, तो अपने मुख्य कार्य में अपने कोड होना चाहिए

Tree test;
test.insert(...)
27/04/2011 को 03:38
का स्रोत उपयोगकर्ता

वोट
1

मैं अपने कोड के कुछ नकल की और यह मेरे लिए ठीक काम कर रहा है:

मुख्य:

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

फ़ंक्शन सम्मिलित करें:

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

अन्य कि तुलना में आप हर जगह वाक्यविन्यास त्रुटियों की है। मैं भी नाम है क्योंकि के रूप में किसी को वहाँ एक नामकरण समस्या का एक सा था ने बताया बदल दिया है। क्योंकि आप इसे हर प्रत्यावर्तन पर बाईं या दाईं सबट्री की जड़ से गुजर रहे हैं CurrentRoot समझ में आता है।

27/04/2011 को 03:39
का स्रोत उपयोगकर्ता

वोट
0

आप जोड़ना नहीं करना चाहिए tree.cppअपने निर्माण आदेश के लिए?

[Trinhc @ CS1 Assignment_3] $ जी ++ movieList.cpp -ओ a.out

बन जाएगा

[Trinhc @ CS1 Assignment_3] $ जी ++ tree.cpp movieList.cpp -ओ a.out

27/04/2011 को 05:01
का स्रोत उपयोगकर्ता

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