vložit na první BST dává konce no-void funkce errror?

hlasů
1

Takže já jsem se snaží naučit, jak kódovat svůj první BST, a to je těžké .... Už jsem potíže s jen pár řádků kódu. Problém je ve vložce, ale jsem zařadil všechno tak, že bych mohl dostat nějakou zpětnou vazbu na můj styl / jiné chyby. Byl jsem navrhl použít ukazatel na realizaci ukazatele, ale my havent ještě nenaučili to, takže já dont pocit pohodlí / vědět, jak ji ještě kód.

chyba je

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

Soubor 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;
}
Položena 27/04/2011 v 05:26
zdroj uživatelem
V jiných jazycích...                            


2 odpovědí

hlasů
1

Co takhle:

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

Všechny pobočky muset vrátit hodnotu (boolean v tomto případě).

Odpovězeno 27/04/2011 v 05:29
zdroj uživatelem

hlasů
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 jen říká, že nechcete vrátit něco z každé možné cestě. Zkuste to:

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);
//  ^^^^^^
}
Odpovězeno 27/04/2011 v 05:29
zdroj uživatelem

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