se introduce pe primul capăt al BST dând nici-nule funcție corectarea erorii?

voturi
1

Deci, eu sunt încercarea de a învăța cum să cod primul meu BST, și este greu .... Sunt care au deja probleme cu doar câteva linii de coduri. problema este în insera, dar am inclus tot, astfel încât am putea obține un feedback pe stilul meu / alte erori. Am fost sugerat de a utiliza un pointer la punerea în aplicare pointer, dar am havent învățat încă, așa că am dont simt confort / știu cum să-l cod încă.

eroare este

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

fișierul 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;
}
Întrebat 27/04/2011 la 05:26
sursa de către utilizator
În alte limbi...                            


2 răspunsuri

voturi
1

Ce zici de:

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

Toate sucursalele trebuie să returneze o valoare (boolean, în acest caz).

Publicat 27/04/2011 la 05:29
sursa de către utilizator

voturi
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

Acest lucru pur și simplu spune că nu întoarce ceva pe orice cale posibilă. Incearca asta:

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);
//  ^^^^^^
}
Publicat 27/04/2011 la 05:29
sursa de către utilizator

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