Returning a Pointer to a Struct

S

Spiffy

When I created a function to return a pointer to a Struct, GCC returns
"error: cannot convert ‘linklist::node*’ to ‘node linklist::*’ in
return" about the return line. I have tried a bunch of things to no
avail. This is a three part question.
1) What it would take to get the code below to compile?
2) How come within this function, I needed to specify class name
when referring to the Struct, while other public or private function
doesn't have to?
3) Ideally, I would keep the Struct private, but that caused more
compiler error. Is this possible?

Thanks!

class linklist {
public:
struct node {
int data;
node *link;
};
linklist();
~linklist();
void append( int num );
private:
struct node *p;
struct node *addnode( int num );
};


struct node linklist::*addnode ( int num ) {
linklist::node *t;
// cut...
return (linklist::node*)t;
}

void linklist::append( int num ) {
 
S

Spiffy

Thought that I add that using a plain version of addnode as I did
below, get the error message "invalid use of undefined type ‘struct
node’" and "forward declaration of ‘struct node’"

struct node *addnode ( int num ) {
node *t;
t = new node;
t->data = num;
return (node*)t;
}
 
I

Ian Collins

Spiffy said:
When I created a function to return a pointer to a Struct, GCC returns
"error: cannot convert ‘linklist::node*’ to ‘node linklist::*’ in
return" about the return line. I have tried a bunch of things to no
avail. This is a three part question.
1) What it would take to get the code below to compile?

Make the definition signature match the declaration!

linklist::node* linklist::addnode( int num )
{
linklist::node *t;
// cut...
return t;
}
2) How come within this function, I needed to specify class name
when referring to the Struct, while other public or private function
doesn't have to?
Eh?

3) Ideally, I would keep the Struct private, but that caused more
compiler error. Is this possible?

If it's private, you can't declare a pointer to one..
 
P

Pascal J. Bourguignon

Spiffy said:
When I created a function to return a pointer to a Struct, GCC returns
"error: cannot convert ‘linklist::node*’ to ‘node linklist::*’ in
return" about the return line. I have tried a bunch of things to no
avail. This is a three part question.
1) What it would take to get the code below to compile?
2) How come within this function, I needed to specify class name
when referring to the Struct, while other public or private function
doesn't have to?
3) Ideally, I would keep the Struct private, but that caused more
compiler error. Is this possible?

Thanks!

class linklist {
public:
struct node {
int data;
node *link;
};
linklist();
~linklist();
void append( int num );
private:
struct node *p;
struct node *addnode( int num );
};


struct node linklist::*addnode ( int num ) {

This is parsed as:

((struct node) linklist::*) addnode ( int num )

The of the result is ((struct node) linklist::*) which means a
pointer to a member of the class linklist, which points to a struct
node.

To return such a type, you would have to declare a member to the class
linklist:

*/
class linklist{
private:
struct node myNode;
/*

and return it:

*/
struct node linklist::*addnode ( int num ) {
return(*myNode);
}
/*

which of course is not what you want, so one wonders why you write
such as return type...

linklist::node *t;
return (linklist::node*)t;

You don't need to qualify the names in the same class as the method in
its body. Of course, this would be true only if it was a method of
that class, and not a normal function. For this, you would have to write:

struct node* linklist::addnode ( int num ) {
node* t=new node();
return(t);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top