简体   繁体   English

geany返回奇怪的结果作为我的二进制搜索树结果的基础C ++

[英]geany returning strange results in the basis for my binary search tree results C++

I began to write a project today to learn more about binary search trees but while writing the class definitions and checking them my accessor was returning false results returning 2 and then on the next line 4196704 in the terminal. 我今天开始编写一个项目,以了解有关二进制搜索树的更多信息,但是在编写类定义并检查它们时,我的访问器返回的错误结果返回2,然后在终端的下一行4196704。 heres the code: 这是代码:

#include <iostream>
using namespace std;

class node
{private:
int data;
node *right,*left;
public:
node();
node(int d,node *r,node *l)
{
    d= data;
    r=right;
    l=left;
}
int nodedata() ;
};
int node::nodedata()
{
    return data;
}
int main()
{

    node root(30,0,0);
    node root2(77,0,0);
    cout<< root.nodedata() << endl;
    cout<< root2.nodedata() << endl;
    return 0;
    }

You are not writing c++ much I guess. 我猜你不是在写C ++。 your definition of variable is wrong: 您对变量的定义是错误的:

d= data;

should be 应该

data = d;

And as for why it shows some werid data in output, those are digits in the actual memory location that your system has put there before you assign this chunck of memory, which you have not yet rewrite, to this object, 至于为什么它在输出中显示一些不完整的数据,这些是在您将此系统尚未分配的内存块分配给该对象之前,系统已放置在系统中实际内存位置的数字,

and for assigning pointers: 和分配指针:

r=right;
l=left;

should be 应该

right= r;
left=l;

and if you wanna access them, put them public or create method to access it eg: 如果您想访问它们,请将它们公开或创建访问它的方法,例如:

cout<<(root.right)-> nodedata() << endl;  //if you make right && left public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM