Serial Monitor display wrong data from pointer

hello everyone.
as i mentioned in the subject when working with pointers,
does anyone run into problems when dealing with pointers of class?
thank you in advance.

#include <Queue.h>
class Node{
  public :
  Node *parentPointer=NULL;
  Node *leftChild=NULL;
  Node *middleChild=NULL;
  Node *rightChild=NULL;
  char dirFromParent;
  char dirInMaze;
  byte ID;
  byte NID;
  bool isDiscovered;
  bool isRoot=false;
};
Node *root=new Node();
Queue<Node*> queue = Queue<Node*>(25);
void printNodeDe(Node *node)
{
  Serial.println(node->dirFromParent);
  Serial.println(node->dirInMaze);
  Serial.println(node->isDiscovered);
  Serial.println(node->ID);
}
void initializeNode(Node *node,char dirFromPar,char dirInMaz,bool isDis,Node *lefChi,Node *midChi,Node *righChi,Node *parPo,int ID,int nid)
{
  node->dirFromParent=dirFromPar;
  node->dirInMaze=dirInMaz;
  node->isDiscovered =isDis;
  node->leftChild = lefChi;
  node->middleChild=midChi;
  node->rightChild = righChi;
  node->parentPointer=parPo;
  node->ID = ID;
  node->NID=nid;
}
void initializeRoot()
{
  initializeNode(root,'a','a',false,NULL,NULL,NULL,NULL,0,0);
  root->isRoot=true;
  queue.push(root);
  
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(38400);
  initializeRoot();
  printNodeDe(queue.front());
}
void loop()
{
}

TheNoneP:
does anyone run into problems when dealing with pointers of class?

No. But then again, you haven't specified what problems you're having. Other than the fact the code you posted in incomplete and won't compile because 'updateID()' is undefined.

gfvalvo:
No. But then again, you haven't specified what problems you're having. Other than the fact the code you posted in incomplete and won't compile because 'updateID()' is undefined.

And, it doesn't have a 'loop()' function.

gfvalvo:
And, it doesn't have a 'loop()' function.

sorry for misleading you, the 'loop()' function is empty in my full code as well as 'updateID()' , the whole work is in 'setup' function.

If you post code and ask for help, it's a good idea to post code that people can copy / paste into the IDE and test. Why make people willing to help you (for free) do extra work?

What happens if you take out:

printNodeDe(queue.front());

And replace it with:

printNodeDe(root);

you right sir , my bad !

i updated the code, i don't want to replace

printNodeDe(queue.front());

with

printNodeDe(root);

because i need that queue to have the nodes , however, when i replace it the serial monitor display the data correctly but if i am using the queue the data is not correct !

TheNoneP:
however, when i replace it the serial monitor display the data correctly but if i am using the queue the data is not correct !

So, the problem is with the queue, not the use of pointers. Looks like you need to dig into the code for that queue library to see if it has an error or you're not using it correctly.

gfvalvo:
So, the problem is with the queue, not the use of pointers.

indeed the problem is from that queue object !

i am not good in library digging ! , the man who post it on github said it is Generic
queue library from github

We still don't know what the code does, or how that differs from what you expect.

Start with turning up your compiler warnings in File --> Preferences. Then, read and understand them them. Look and see what type 'queue.front()' returns. Is the compiler happy passing that type to 'printNodeDe()' or is it just letting you shoot yourself in the foot?

PaulS:
We still don't know what the code does, or how that differs from what you expect.

it is simple, i have a class 'Node' and i declare and initialize a pointer 'root' from that class using 'initializeRoot' function then i added that pointer to 'queue' and tried to print the content of that pointer with 'printNodeDE()' but the content i got is wrong.

TheNoneP:
sorry for misleading you, the 'loop()' function is empty in my full code as well as 'updateID()' , the whole work is in 'setup' function.

Even an empty loop() is an integral part of an Arduino Sketch; however, you may omit its explicit declaration provided you have established a means in the setup function ( like while(1); ) where the Processor is looping around a code.

GolamMostafa:
Even an empty loop() is an integral part of an Arduino Sketch.

if you read the rest of the comments you will find that i updated the code as well as i don't have a problem with 'loop' function

Reply #9 should have hit you like a clue-by-four. To repeat, what data type does 'queue.front()' return?

gfvalvo:
what data type does 'queue.front()' return?

since i declare the queue to store Pointers of Node the queue.front() will return the first pointer in the queue.

gfvalvo:
Look and see what type 'queue.front()' returns. Is the compiler happy passing that type to 'printNodeDe()' or is it just letting you shoot yourself in the foot?

i opened the library code and found this code

T  *_data
inline int front();
template<class T>
inline int Queue<T>::front() 
{
  return _front;
}

i change the front() function prototype and its content to

inline T front();
template<class T>
inline T Queue<T>::front() 
{
  return _data[_front];
}

and saved the changes , now everything good with pointers ! , does that mean the library has a bug ? or i did a work around solution ?

TheNoneP:
does that mean the library has a bug ? or i did a work around solution ?

No, the library doesn't have a bug. You called the wrong function and then kludged the library to make up for it. Have a look at the 'peek()' function.

gfvalvo:
. You called the wrong function and then kludged the library to make up for it. Have a look at the 'peek()' function.

the 'front()' function should always return the first element in the queue and it should return the datatype that the queue declared with it in my case a 'node pointer' ,why it returns int ??

TheNoneP:
the 'front()' function should always return the first element in the queue and it should return the datatype that the queue declared with

The author of the library begs to differ. He chose to have 'front()' return the index of the first element and 'peek()' do what you want. BTW, 'pop()' also returns the first element but also removes it from the queue at the same time.

Don't like those choices? Take it up with the author or write your own library.

well , you are right , its just a " differ in sense " , thank you :slight_smile: