"struct list" why is the one with out typedef not working?

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

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
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;
//typedef struct Lista * list;

struct Lista
{
  int data;
   struct Lista next=0;
};

void insert(struct Lista&l,int data);

int main()
{
  struct Lista l=0;
  int d;

  while(cin>>d)
    {
      insert(l,d);
    }

  return 0;
}
void insert(struct Lista&l,int data)
{

 struct list newlist=new Lista;
  newlist->data=data;
  newlist->next=l;

}


working code with typedef



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
26
27
28
29
30
31
#include <iostream>

using namespace std;
typedef struct Lista * list;

struct Lista
{
  int data;
 list next=0;
};

void insert( list&l,int data);

int main()
{
   list l=0;
  int d;

  while(cin>>d)
    {
      insert(l,d);
    }

  return 0;
}
void insert( list&l,int data)
{
  list newlist=new Lista;
  newlist->data=data;
  newlist->next=l;
}



Last edited on
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.

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
26
27
28
29
30
31
32
33
#include <iostream>

using namespace 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?;>
Last edited on
Topic archived. No new replies allowed.