So I want to add a struct from a c header file as a class member to a c++ class.
But I get a compiler error for the cpp file: bar was not declared inn thi
Let us consider an example. Consider a Linked List in which each node is represented by:
struct ListNode {
int val;
ListNode *next;
ListNode(int x){
val = x;
next = NULL;
}
};
To initialize a node with value 5 and next pointer pointing to NULL you can write the code snippet as:
ListNode node = new ListNode();
node.val = 5;
node.next = NULL;
or
ListNode node = new ListNode(5);
Another fancy declaration can be made as
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
Here ListNode(int x): val(x), next(NULL) is a constructor which initializes the value of the struct ListNode.
Hope this make things more clear and easy. :)