Pointer problem

Hi.
I'm new to C++ and is currently having a problem.
I have done the following get method:

1
2
3
4
dagnode* dag::getVersionNode(int version){
	dagnode *res = &dagnodeMap[version];
	return *res;
}


But get the following error:
../DAG/dag.cpp:64:10: error: cannot convert ‘dagnode’ to ‘dagnode*’ in return

But i dont get it. *res is a pointer to a dagnode, and I want to return a pointer to a dagnode. So it should be correct?
Instead of return *res; you shall write return res;
res is a pointer to a dagnode. *res is the value of that dagnode.

* is the dereference operator which returns the value pointed to by a pointer. But it's also used to declare pointers, which is where your confusion is probably coming from.
Hey everybody.
Okay i dont exacly understand why but it works.

However i now get a problem when i call it:

dagnode search = globalDag->getVersionNode(1);

../test.cpp:46:46: error: conversion from ‘dagnode*’ to non-scalar type ‘dagnode’ requested
../test.cpp:47:45: error: base operand of ‘->’ has non-pointer type ‘dagnode’

Can you see whats wrong?
Wait. I got it. I should just say:

dagnode *search = globalDag->getVersionNode(1);

And the reason why is that we want to set the source and not the pointer?
getVersion Node(x) returns a pointer, so you need to assign the returned value to a pointer. If you just want a value you could do something like this:
 
dagnode search = *(globalDag->getVersionNode(1));

Or you could return the value instead of a pointer from getVersionNode().
Your second error comes from the fact that globalDag is a dagnode, not a *dagnode. To call member functions you only use '->' for pointers, otherwise use '.' :
 
dagnode search = globalDag.getVersionNode(1);
Topic archived. No new replies allowed.