Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ 在托管C++; 我试图找出如何在管理C++中创建一个用于学校项目的二叉树类。我在非托管C++和C语言中找到了一些很好的例子,所以我能够很好地理解正在发生的事情,但是我似乎无法在托管C++中找到答案。我想了解的是:为什么会出现堆栈溢出(见下文),这是一种明智的方法吗?这是我的班级: #pragma once #include "stdafx.h" #include <iostream> #include <deque> #include <climits> using namespace std; using namespace System; using namespace System::Collections; ref struct Node { int data; Node ^parent; Node ^left; Node ^right; // constructors // default constructor Node (){} // constructor that takes a node with no leafs // a constructor that accepts a new node with no children Node(int input) { Node ^node = gcnew Node(input); node->data =input; node->left = nullptr; node->right = nullptr; node->parent = nullptr; } // method to create a new Node Node ^newNode(int data) { Node ^node = gcnew Node; node->data = data; node->left = nullptr; node->right = nullptr; node->parent = nullptr; return node; } // method that inserts a new node into an existing tree Node ^insertNode(Node ^node, int input) { Node ^p; Node ^returnNode; if (node == nullptr) { returnNode = newNode(input); returnNode->parent = p; return returnNode; } if (input <= node->data) { p = node; node->left = insertNode(node->left, input); } else { p = node; node->right = insertNode(node->right, input); } return node; } };_Visual C++_Binary Tree - Fatal编程技术网

Visual c++ 在托管C++; 我试图找出如何在管理C++中创建一个用于学校项目的二叉树类。我在非托管C++和C语言中找到了一些很好的例子,所以我能够很好地理解正在发生的事情,但是我似乎无法在托管C++中找到答案。我想了解的是:为什么会出现堆栈溢出(见下文),这是一种明智的方法吗?这是我的班级: #pragma once #include "stdafx.h" #include <iostream> #include <deque> #include <climits> using namespace std; using namespace System; using namespace System::Collections; ref struct Node { int data; Node ^parent; Node ^left; Node ^right; // constructors // default constructor Node (){} // constructor that takes a node with no leafs // a constructor that accepts a new node with no children Node(int input) { Node ^node = gcnew Node(input); node->data =input; node->left = nullptr; node->right = nullptr; node->parent = nullptr; } // method to create a new Node Node ^newNode(int data) { Node ^node = gcnew Node; node->data = data; node->left = nullptr; node->right = nullptr; node->parent = nullptr; return node; } // method that inserts a new node into an existing tree Node ^insertNode(Node ^node, int input) { Node ^p; Node ^returnNode; if (node == nullptr) { returnNode = newNode(input); returnNode->parent = p; return returnNode; } if (input <= node->data) { p = node; node->left = insertNode(node->left, input); } else { p = node; node->right = insertNode(node->right, input); } return node; } };

Visual c++ 在托管C++; 我试图找出如何在管理C++中创建一个用于学校项目的二叉树类。我在非托管C++和C语言中找到了一些很好的例子,所以我能够很好地理解正在发生的事情,但是我似乎无法在托管C++中找到答案。我想了解的是:为什么会出现堆栈溢出(见下文),这是一种明智的方法吗?这是我的班级: #pragma once #include "stdafx.h" #include <iostream> #include <deque> #include <climits> using namespace std; using namespace System; using namespace System::Collections; ref struct Node { int data; Node ^parent; Node ^left; Node ^right; // constructors // default constructor Node (){} // constructor that takes a node with no leafs // a constructor that accepts a new node with no children Node(int input) { Node ^node = gcnew Node(input); node->data =input; node->left = nullptr; node->right = nullptr; node->parent = nullptr; } // method to create a new Node Node ^newNode(int data) { Node ^node = gcnew Node; node->data = data; node->left = nullptr; node->right = nullptr; node->parent = nullptr; return node; } // method that inserts a new node into an existing tree Node ^insertNode(Node ^node, int input) { Node ^p; Node ^returnNode; if (node == nullptr) { returnNode = newNode(input); returnNode->parent = p; return returnNode; } if (input <= node->data) { p = node; node->left = insertNode(node->left, input); } else { p = node; node->right = insertNode(node->right, input); } return node; } };,visual-c++,binary-tree,Visual C++,Binary Tree,在节点中构造函数中 Node(int input) { Node ^node = gcnew Node(input); …您无条件地调用节点构造函数 Node(int input) { Node ^node = gcnew Node(input); 这不会有好的结局。为什么要在节点构造函数中构造新的节点?调用节点构造函数时,会调用它来构造*此对象——您应该初始化当前实例,而不是创建新的节点 同样地,在insertNode成员函数中——它已经可以访问*这个对象;无需通过单独

节点中
构造函数中

Node(int input)
{
    Node ^node = gcnew Node(input);
…您无条件地调用
节点
构造函数

Node(int input)
{
    Node ^node = gcnew Node(input);
这不会有好的结局。为什么要在
节点
构造函数中构造新的
节点
?调用
节点
构造函数时,会调用它来构造
*此
对象——您应该初始化当前实例,而不是创建新的
节点


同样地,在
insertNode
成员函数中——它已经可以访问
*这个
对象;无需通过单独的参数进行传递。

谢谢。学习两种不同的语言,并在学习过程中相互对视!:)