returning structs
Aug 17, 2013 at 10:49pm UTC
Hi everyone.
First of all, I'm new around c++ so I would apreciate any given help!
I'm creating a linked list:
This is the List.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class List{
private :
typedef struct node {
int data;
node* next;
}* nodePtr;
nodePtr head;
nodePtr iterador;
public :
List();
void addHead(nodePtr celd);// Here I want to receive a struct node -
//data type
struct node* createCeld(int value);
void printList();
};
#endif // LIST_H
1. In the function name createCeld, I made a celd named newCeld and I wanted to return that newCeld, but I was not able t return the struct node*
I think that we cannot return a struct, only pointers; if so, how can i return a pointer holding that celd which type is a struct ?
Any idea of what my mistake is?
Thanks!
Aug 17, 2013 at 10:55pm UTC
I think that we cannot return a struct, only pointers
You can return anything*, so it is perfectly legal to return a struct. However, you probably would not want to return a struct here, because it would be difficult/impossible to put that struct into the linked list. It really makes more sense to return a pointer.
Since, by the code you posted, you already appear to be returning a pointer, you'll have to be more specific about the problem before I can offer any more help. I assume your createCeld function is giving you an error.... if so, what is the error? And can you post the body of your createCeld function?
* except a C style array
Aug 17, 2013 at 11:10pm UTC
Thanks for replying !
Again, I'm noob so don't be mean please haha
1 2 3 4 5 6 7 8 9 10
nodePtr List::createCeld(int value) {
nodePtr celd= new node;
celd-> data = value;
celd-> next= NULL;
return celd;
}
And, would you explain why would it be difficult/impossible to put that struct into the linked list, please?
Aug 17, 2013 at 11:36pm UTC
When I compile it, is says:
error: 'nodePtr' does not name a type|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 1 seconds) ===|
So it says the error is in this line:
1 2 3 4 5 6 7 8
nodePtr List::createCeld(int value) {
nodePtr celd= new node;
celd-> data = value;
celd-> next= NULL;
return celd;
}
So I dont have any idea of what to do!
Aug 18, 2013 at 12:07am UTC
nodePtr is inside the List class so you must use the scope operator to specify it:
List::nodePtr
Aug 18, 2013 at 12:13am UTC
So it would be like this:
1 2 3 4 5 6 7 8
List ::nodePtr List::createCeld(int value) {
nodePtr celd= new node;
celd-> data = value;
celd-> next= NULL;
return celd;
}
Where List ::nodePtr is the return type that specifies that we will be returning a nodePtr type which is in the List class?
By that, I'm returning a pointer right?
And by this:
void addHead(nodePtr celd)
Am I passing a pointer named celd, right?
Thanks a lot.
Aug 18, 2013 at 5:31am UTC
Where List ::nodePtr is the return type that specifies that we will be returning a nodePtr type which is in the List class?
By that, I'm returning a pointer right?
Yes, that is correct.
Am I passing a pointer named celd, right?
Yes.
Topic archived. No new replies allowed.