I have an error in my project and I can't fix the problem

#pragma once

class Node
{
	int code;
	int count;
	Node* parent;
	Node* rightChild;
    Node* leftChild;

public:
	Node(int c1,int c2);
	void setCode(int c);
    void setCounter(int c);
	void setRightChild(Node n);
	void setleftChild(Node n);
	void setParent(Node n);

	int getCode();
	int getCounter();
	Node getRightChild();
	Node getleftChild();
	Node getParent();


	
	Node(void);
	~Node(void);
};
#include "Node.h"

Node::Node(void)
{
}

Node::~Node(void)
{
	//delete this;
	
}
Node::Node(int c1, int c2){
	code=c1;
	count=c2;
	parent=0;
	rightChild=0;
	leftChild=0;
}

int Node::getCounter()
{
	return count;
}
Node Node::getleftChild()
{
	return(*leftChild);
}
Node Node::getRightChild()
{
	return (*rightChild);
}
Node Node::getParent()
{
	return (*parent);
}
void Node::setCode(int c)
{
	code=c;
}

void Node::setCounter(int c)
{
	count=c;
}
 void Node::setleftChild(Node n1)
{
	leftChild=&n1;
}
void Node::setRightChild(Node n2)
{
	rightChild=&n2;
}
void Node::setParent(Node n)
{
	parent=&n;
}
int Node::getCode(){
	return code;
}
#include"Elem.h"
using namespace std;
int main(){

 	//Node n2(3,4);
	Node n3(5,6);
	Node n4(7,8);
	Node n5(9,10);
     n3.setRightChild(n4);
	n3.setleftChild(n5);
return 0;
}

my problem is when I setrightchild of n3.(with debug it's changed)

but when I setleftchild (with debug the rightleftchild also changes and the left also changes to the same numbers

I need any help in this previuose

any help will be accepted.

i think it is becuase you are passing copies of the nodes to the setRightChild setleftChild functions. not references (or pointers)
so instead of this

void Node::setleftChild(Node n1)
{
	leftChild=&n1;
}
void Node::setRightChild(Node n2)
{
	rightChild=&n2;
}

try

void Node::setleftChild(Node* n1)
{
	leftChild=n1;
}
void Node::setRightChild(Node* n2)
{
	rightChild=n2;
}

...

n3.setRightChild(&n4);
n3.setleftChild(&n5);

in debug mode maybe the copies are not deleted after the function returns and so there is no error. to test this see if the addresses of n3 and n5 match the pointers to them in n4

yup
you are right.It's run now.
But I want to know the reason which causes this problem
I mean what's the difference between send a pointer and send a node.

a pointer (or reference) is like a sign saying "this is where ... is".
as you (should) know any variable you create is allocated in ram and given an address so the program knows where to find it.

if you point pointer to a variable then you can use the pointer as a variable its self eg you can print it with cout, point it to somewhere else but you can also access the variable through it. a reference is like a pointer but you cant treat it as a variable. you can only access the data it points to (so think of (type& x) as an alias and (type* x) as a sign showing the function where to find x)

so if you have void function(type x) { return; } and you call it like so function(AnObject); a copy is given to the function


sorry if im going a bit too in depth and ask if u don't get something. and don't forget to mark as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.