wstawić na pierwszy BST dając koniec no-void function errror?

głosy
1

Więc ja próbuje nauczyć się kodować swój pierwszy BST, i trudno .... mam już problemów z zaledwie kilku linii kodu. problem jest we wkładce, ale mam włączone wszystko, abym mógł uzyskać informację zwrotną na temat mojego stylu / inne błędy. Byłem zalecane, aby użyć wskaźnika do realizacji wskaźnika, ale nie miałaś dowiedział się jeszcze, więc nie czujesz komfort / wie jak zakodować go jeszcze.

błąd jest

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

plik 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;
}
Utwórz 27/04/2011 o 05:26
źródło użytkownik
W innych językach...                            


2 odpowiedzi

głosy
1

Co powiesz na:

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

Wszystkie oddziały muszą zwrócić wartość logiczną (w tym przypadku).

Odpowiedział 27/04/2011 o 05:29
źródło użytkownik

głosy
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

To po prostu mówi, że nie wrócisz coś na każdą możliwą ścieżkę. Spróbuj tego:

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);
//  ^^^^^^
}
Odpowiedział 27/04/2011 o 05:29
źródło użytkownik

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