नो-शून्य समारोह errror के पहले BST अंत देने पर डालने?

वोट
1

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

त्रुटि है

cc1plus: warnings being treated as errors
tree.cpp: In member function âbool Tree::insert(Tree::Node*&, int, std::string)â:
tree.cpp:34: warning: control reaches end of non-void function

tree.h फ़ाइल

#ifndef TREE_H
#define TREE_H

#include <iostream>
#include <string>
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*& 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);
}

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 को 05:26
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


2 जवाब

वोट
1

कैसा रहेगा:

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

सभी शाखाओं (इस मामले में बुलियन) एक मूल्य के वापस जाने के लिए की जरूरत है।

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

वोट
4

cc1plus: warnings being treated as errors
tree.cpp: In member function âbool Tree::insert(Tree::Node*&, int, std::string)â:
tree.cpp:34: warning: control reaches end of non-void function

यह सिर्फ कहता है कि आप हर संभव रास्ते पर कुछ नहीं लौटाते हैं। इसे इस्तेमाल करे:

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)
    return insert(currentRoot->left, k, s);
//  ^^^^^^
  else
    return insert (currentRoot->right,k, s);
//  ^^^^^^
}
27/04/2011 को 05:29
का स्रोत उपयोगकर्ता

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