τοποθετήστε την πρώτη BST δίνοντας τέλος μη κενό errror λειτουργία;

ψήφοι
1

Είμαι, λοιπόν, προσπαθούν να μάθουν πώς να κώδικα πρώτο μου BST, και είναι δύσκολο .... Είμαι ήδη πρόβλημα με λίγες μόνο γραμμές των κωδικών. το πρόβλημα είναι στο ένθετο, αλλά έχω συμπεριλάβει τα πάντα, έτσι ώστε θα μπορούσα να πάρω κάποια ανατροφοδότηση σχετικά με το ύφος / άλλα λάθη μου. Ήμουν πρότεινε να χρησιμοποιήσει ένα δείκτη για την εφαρμογή του δείκτη, αλλά εμείς havent μάθει ακόμα, γι 'αυτό dont αίσθηση άνεσης / ξέρουν πώς να το κωδικοποιήσει ακόμα. ο

σφάλμα είναι

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

Όλα τα κλαδιά πρέπει να επιστρέψει μια τιμή (boolean σε αυτή την περίπτωση).

Απαντήθηκε 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