helyezze az első BST amely végén nem-void függvény errror?

szavazat
1

Tehát én próbál tanulni, hogyan kell kódolni az első BST, és ez nehéz .... én már gondjai csak néhány sornyi kódot. A probléma az, hogy a betét, de én mindent beletettünk, hogy tudtam egy kis visszajelzést az én stílusom / egyéb hibákat. Azt javasolták, hogy egy mutató pointer végrehajtására, de mi kikötõ tanultam meg, úgyhogy nem érzi kényelem / tudja, hogyan kell a kódot, hogy még. a

hiba

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

A tree.h fájl

#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;
}
A kérdést 27/04/2011 05:26
a forrás felhasználó
Más nyelveken...                            


2 válasz

szavazat
1

Mit szólnál:

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

Minden ág kell vissza értéket (logikai ebben az esetben).

Válaszolt 27/04/2011 05:29
a forrás felhasználó

szavazat
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

Ez csak azt mondja, hogy nem térnek vissza valamit minden lehetséges útját. Próbáld ezt:

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);
//  ^^^^^^
}
Válaszolt 27/04/2011 05:29
a forrás felhasználó

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