采取临时

时间:2015-06-12 02:48:12

标签: c++ pointers

我试图将我的函数的返回值赋给一个节点,这是一个指针。无论是否有'&'我都试过了但他们似乎没有工作。我目前在标题中收到错误。没有'&'得到“无法将节点转换为*节点中的分配”错误

   class Node{

    public:
    int value;
    Node *left, *right;

    Node(int x, Node *l, Node *r){
        value = x;
        left = l;
        right = r;
    }
};

class Tree{

public:
    Node *root;
    Tree(Node *r){
        root = r;
    }

    void add(int num){
       cout << "adding" << endl;
        root = add(root, num);//error occurs here
}

private:
    Node add(Node *parent, int num){

        cout << "im in..." << endl;

        if(parent == root && root == nullptr){
           cout << "root" << endl;
            Node n(num, nullptr, nullptr);
            return n;
        }

        if(parent == nullptr){
            Node next(num, nullptr, nullptr);
            cout << "actually adding" << endl;
            return next;

        }
        else if(num > parent->value){
           cout << "bigger" << endl;
            parent->right = add(parent->right, num);
        }
        else {
            cout << "smaller" << endl;
            parent->left = add(parent->left, num);
        }
    }
};

int main(){

    Node n1(100, nullptr,nullptr);

    Tree tree(&n1);
    tree.add(1);

        return 0;}

2 个答案:

答案 0 :(得分:2)

您提到您是C ++的新手,返回局部变量或局部变量的地址是不安全的。您必须为Shell "cmd.exe /S /C move " & oldName & " " & newName, windowStyle:=vbHide 分配空间,以便安全地返回它,并使用Node完成C ++。分配有new的对象将一直存在,直到被删除为止(修改:为清楚起见,我的意思是在new上使用delete之前)

下面我修改了你的代码,并在我做出改变的地方放置了评论。你的代码中有一些条件可能没有返回任何内容(这很糟糕,因为函数应该返回一些内容):

Node*

<强>输出

#include <iostream>
using namespace std;

class Node {
public:
    int value;
    Node* left, * right;

    Node(int x, Node* l, Node* r) {
        value = x;
        left = l;
        right = r;
    }
};

class Tree {
public:
    Node* root;

    Tree(Node *r) {
        root = r;
    }

    void add(int num) {
        cout << "adding" << endl;
        root = add(root, num);
    }

private:
    // ---------------------------------
    // Return a Node* instead of a Node
    // ---------------------------------
    //Node add(Node* parent, int num) {
    Node* add(Node* parent, int num) {

        cout << "im in..." << endl;

        if (parent == root && root == nullptr) {
            cout << "root" << endl;

            //Node n(num, nullptr, nullptr);
            //return n;

            // ------------------------------------
            // Return a new Node instead of a Node
            // ------------------------------------
            return new Node(num, nullptr, nullptr);
        }

        if (parent == nullptr) {
            //Node next(num, nullptr, nullptr);
            cout << "actually adding" << endl;
            //return next;

            // ------------------------------------
            // Return a new Node instead of a Node
            // ------------------------------------
            return new Node(num, nullptr, nullptr);
        }
        else if (num > parent->value) {
            cout << "bigger" << endl;
            parent->right = add(parent->right, num);

            // ------------------------------------
            // You are not returning anything here
            // Probably should return parent
            // ------------------------------------
            return parent;
        }
        else {
            cout << "smaller" << endl;
            parent->left = add(parent->left, num);

            // ------------------------------------
            // You are not returning anything here
            // Probably should return parent
            // ------------------------------------
            return parent;
        }
    }
};

int main() {
    Node n1(100, nullptr, nullptr);

    Tree tree(&n1);
    tree.add(1);

    return 0;
}

答案 1 :(得分:0)

正如其他人所说,你需要让Tree::add()返回一个Node*指针,并让它动态分配正在添加的新Node

但是,其他人没有告诉您的是,在现有Tree::add()旁边插入新的Node时,您的Node实施工作严重受损。您只是部分更新现有的Nodes,以引用现在位于其旁边的新Node

尝试更像这样的东西:

class Node
{
public:
    int value;
    Node *left, *right;

    Node(int x, Node *l = nullptr, Node *r = nullptr)
        : value(x), left(l), right(r)
    {
    }
};

class Tree
{
public:
    Node *root;

    Tree()
        : root(nullptr)
    {
    }

    ~Tree()
    {
        Node *n = root;
        while (n != nullptr)
        {
            Node *n2 = n->right;
            delete n;
            n = n2;
        }
    }

    void add(int num)
    {
        cout << "adding" << endl;
        add(root, num);
    }

private:
    Node* add(Node *refNode, int num)
    {
        cout << "im in..." << endl;

        if (refNode == nullptr)
        {
            if (root == nullptr)
            {
                cout << "root" << endl;
                root = new Node(num);
                return root;
            }

            cout << "actually adding" << endl;
            refNode = root;
        }

        Node *newNode;

        if (num >= refNode->value)
        {
            cout << "bigger or equal" << endl;

            Node *n = refNode;
            while ((n->right != nullptr) && (num >= n->right->value))
                n = n->right;

            newNode = new Node(num, n, n->right);

            if (n->right != nullptr)
                n->right->left = newNode;
            n->right = newNode;
        }
        else
        {
            cout << "smaller" << endl;

            Node *n = refNode;
            while ((n->left != nullptr) && (num < n->left->value))
                n = n->left;

            newNode = new Node(num, n->left, n);

            if (n->left != nullptr)
                n->left->right = newNode;
            n->left = newNode;

            if (n == root)
                root = newNode;
        }

        return newNode;
    }
};

int main()
{
    Tree tree;
    tree.add(100);
    tree.add(1);

    return 0;
}

话虽如此,你真的应该使用std::list类,让它为你管理节点:

#include <list>

class Tree
{
public:
    std::list<int> nodes;

    void add(int num)
    {
        cout << "adding" << endl;

        if (nodes.empty())
        {
            cout << "root" << endl;
            nodes.push_back(num);
            return;
        }

        cout << "actually adding" << endl;

        std::list<int>::iterator iter = nodes.begin();

        if (num >= *iter)
        {
            cout << "bigger or equal" << endl;

            ++iter;
            while ((iter != nodes.end()) && (num >= *iter))
                ++iter;

            nodes.insert(iter, num);
        }
        else
        {
            cout << "smaller" << endl;

            while ((iter != nodes.begin()) && (num < *(iter-1)))
                --iter;

            nodes.insert(iter, num);
        }
    }
};

int main()
{
    Tree tree;
    tree.add(100);
    tree.add(1);

    return 0;
}