i'm working/trying to learn about "struct list".
First I tried to do without typedef without success getting the following compilation error the code is for it is just under.
my question is why the the first one do not work? and what do I have to change to make it work without making typedef. the working code I will include as well.
$ g++-4.7 -Wall -Wextra -pedantic -std=c++11 test1.cc
test1.cc:9:22: error: field ‘next’ has incomplete type
test1.cc: In function ‘int main()’:
test1.cc:16:18: error: conversion from ‘int’ to non-scalar type ‘Lista’ requested
test1.cc: In function ‘void insert(Lista&, int)’:
test1.cc:29:14: error: variable ‘insert(Lista&, int)::list newlist’ has initializer but incomplete type
Wow, you just messed it all up... version with typedef have a pointer to Lista, and other example is an instance.., then you try this struct list newlist=new Lista;new return pointer for newly crated Lista and you are trying assign it to instance.
#include <iostream>
usingnamespace std;
//typedef struct Lista * list;
struct Lista
{
int data;
struct Lista *next;
};
void insert(struct Lista&l,int data);
int main()
{
struct Lista l;
int d;
while(cin>>d)
{
insert(l,d);
}
return 0;
}
void insert(struct Lista &l,int data)
{
struct Lista *newlist=new Lista;
newlist->data=data;
newlist->next=&l;
}
PS. you must test it yourself, because i simply changed what compiler found wrong.
ps2. and did you try to change data in that struct, or create new struct and chain it with others?;>