I'm trying to understeand how to use structs as lists in C++. I came up with a piece of code that to my understeanding should not result in any errors, but it did..
My code is this:
struct item {
int data;
struct item *next;
};
struct item *begin = NULL;
void add(int x) {
struct item *a = new struct item();
a->data = x;
a->next = begin;
begin = a;
}
int main() {
add(2);
printf("%d\n", begin->data);
return 0;
}
and it gives me this:
Undefined symbols for architecture x86_64:
"operator new(unsigned long)", referenced from:
add(int) in structtest-f49486.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I use GCC inside my mac terminal to run my code. I haven't seen this type of error before. I have found that the error is not present when I remove the line
struct item *a = new struct item();
Can anyone tell me what's wrong here?
Thanks,
Merijn