link list

so im trying to make this link list and it keeps telling me that LinkList is a struct

any way heres my code maby some one will see what i don't i would love to debug it further im sure there some errors of some type but i cant get past this one to be able to debug more
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef LINKLIST_H
#define LINKLIST_H
#include <string>
#include <iostream> 
   
class LinkList()
{
      private:
          struct data{
           string class;
           string number;
           string hours;
           };
          
          struct node{
           node *next;
           data y;
           };
           node *starting;
       public: 
              LinkList();   
              void insertAtEnd();
              bool isEmpty() const;
              
};

LinkList::LinkList()
{starting = NULL;}

void LinkList::insertAtEnd()
{
                 node *temp, *temp2;
                 temp = new node;
                 cout << "Enter class name: " << endl;
                 cin >>  temp->y->class;
                 cout << "Enter class number: " << endl;
                 cin >> temp->y->number;
                 cout << "Enter class hours: " << endl;
                 cin >> temp->y->hours;
                 temp->next = NULL;
                 if(starting == NULL)
                 {                 
                                   starting = temp;
                                   }
                 else{
                      temp2 = starting;
                      while(starting->next != NULL)
                      {
                                           temp2 = temp2->next;
                                           }
                      temp2->next = temp;
                      
                      }
                      return;
}



#endif


1. Class declaration should be
 
class LinkList { } ;

and not
 
class LinkList() { }; 


2. In string class
class is a data type, you cannot use it as a name for your string.

3. In temp->y->number use temp->y.number
y is an object and not a pointer. For pointer you use -> operator

Here is your corrected code

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef LINKLIST_H
#define LINKLIST_H
#include <string>
#include <iostream>
   using namespace std;
class LinkList
{
      private:
          struct data{
           string class1;
           string number;
           string hours;
           };

          struct node{
           node *next;
           data y;
           };
           node *starting;
       public:
              LinkList();
              void insertAtEnd();
              bool isEmpty() const;

};

LinkList::LinkList()
{starting = NULL;}

void LinkList::insertAtEnd()
{
                 node *temp, *temp2;
                 temp = new node;
                 cout << "Enter class name: " << endl;
                 cin >>  temp->y.class1;
                 cout << "Enter class number: " << endl;
                 cin >> temp->y.number;
                 cout << "Enter class hours: " << endl;
                 cin >> temp->y.hours;
                 temp->next = NULL;
        temp->next = NULL;
                 if(starting == NULL)
                 {
                                   starting = temp;
                                   }
                 else{
                      temp2 = starting;
                      while(starting->next != NULL)
                      {
                                           temp2 = temp2->next;
                                           }
                      temp2->next = temp;

                      }
                      return;
}



#endif


Hope this helps
Last edited on
lul dam i feal like a fool now thanks
I have edited my post above.
lul thanks im still new to c++

but still not "correct" got some debuging to do in that apparently

man im having a bad night for coding just all muddled
Last edited on
Why? What's the error?
when running:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include "LinkList.h"
using namespace std;

int main()
{
    LinkList listz();
    listz.insertAtEnd();
}


to test i get

9 C:\Users\CrimsonOne\Desktop\testLinkage.cpp request for member `insertAtEnd' in `listz', which is of non-class type `LinkList ()()'
Change

LinkList listz(); to LinkList listz;

Last edited on
ok thats the first error that dusnt seam like me having just a retarded night could you explain a bit?
LinkList listz(); is not the declaration of an object.

Object of any class or any struct is defined by

classname objectname;

Of course there are other ways with constructors and pointers
Last edited on
thanks a lot you made my shitty night a lot less shitty ^ ^

lul its like i forgot how to program for a bit ther lol
ha ha ha
Glad I could help !

That's "C++'s most vexing parse" according to Scott Meyers in Effective C++. The expression is parsed as a function declaration named listz that takes no arguments and returns a LinkList...the compiler can't tell if it is supposed to be a function or a variable instantiation, so it treats it as a function to remain compatible with C.
So heres the updated code:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#include <string>
#include <iostream> 
using namespace std;   
   
class LinkList
{
      private:
          struct data{
           string Class;
           string number;
           string hours;
           };
          
          struct node{
           node *next;
           data y;
           };
           node *starting;
       public: 
              LinkList();   
              void show();
              void insertAtEnd();
              void Insert(int num);
              bool isEmpty() const;
              
};

LinkList::LinkList()
{starting = NULL;}

void LinkList::insertAtEnd()
{
                 node *temp, *temp2;
                 temp = new node;
                 cout << "Enter class name: " << endl;
                 cin >>  temp->y.Class;
                 cout << "Enter class number: " << endl;
                 cin >> temp->y.number;
                 cout << "Enter class hours: " << endl;
                 cin >> temp->y.hours;
                 temp->next = NULL;
                 if(starting == NULL)
                 {                 
                                   starting = temp;
                                   }
                 else{
                      temp2 = starting;
                      while(starting->next != NULL)
                      {
                                           temp2 = temp2->next;
                                           }
                      temp2->next = temp;
                      
                      }
                      return;
}
void LinkList::Insert(int num)
{
     node *temp, *temp2;
     
     for(int i = 0; temp=starting,i<num;i++)
     {
             temp = temp->next;
             
             if(temp = NULL)
             {
                      cout << "there arent " << num << "elements in the array" << endl;
                      return;
                      }
             
             }
      temp2 = new node;
      cout << "Enter class name: " << endl;
      cin >>  temp2->y.Class;
      cout << "Enter class number: " << endl;
      cin >> temp2->y.number;
      cout << "Enter class hours: " << endl;
      cin >> temp2->y.hours;
      temp2->next = NULL;  
     
      return;
 }



void LinkList::show()
{
     node *temp;
     int ip;
     cout<< "\n---------------------- printing classes --------------------------";
     for(temp=starting, ip = 0; temp != NULL; temp = temp->next, ip++)
     {
                        cout << "\n" << ip << ") " << temp->y.Class;
                        }
     cout << "\n------------------------------------------------------------------\n";
 }


#endif 


it seams i cant insert and when ever i try and make more than 2 nodes the whole thing fails... im not sure why

ok so i found out the why but how do you fix a Segmentation fault? (tryed running in vi and thats what i got)

Last edited on
for (int i=0, temp = starting; i<num; i++) and not for (int i=0; temp=starting, i<end;i++)
thanks but i still for some reason cant ever make a third entry on the list
Last edited on
In the InsertAtEnd() function, the following loop does not
read right:
1
2
3
4
5
6
7
8
                      temp2 = starting;
                      while(starting->next != NULL) //this line doesn't seem right
                      {
                                           temp2 = temp2->next;
                                           }
                      temp2->next = temp;
                      
                      }


What about:
1
2
3
4
5
6
7
8
                      temp2 = starting;
                      while(temp2->next != NULL) //should be this??
                      {
                                           temp2 = temp2->next;
                                           }
                      temp2->next = temp;
                      
                      }
According to me your Insert code should be like this

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
34
35
36
37
38
39
40
void LinkList::Insert(int num)
{
     node **temp, *temp2;
     temp = &starting;
     temp2 = *temp;
     if (temp2 == NULL ) {
 
         temp2 = new node;
       cout << "Enter class name: " << endl;
      cin >>  temp2->y.Class;
      cout << "Enter class number: " << endl;
      cin >> temp2->y.number;
      cout << "Enter class hours: " << endl;
      cin >> temp2->y.hours;
      temp2->next = NULL;
      starting = temp2;

     }
     else {
         while (temp2->next != NULL)
     {
             temp2 = temp2->next;


       }
      temp2->next = new node;
      temp2 = temp2->next;
      cout << "Enter class name: " << endl;
      cin >>  temp2->y.Class;
      cout << "Enter class number: " << endl;
      cin >> temp2->y.number;
      cout << "Enter class hours: " << endl;
      cin >> temp2->y.hours;
      temp2->next = NULL;


 }

}


Problem with your code was that your linked lists head was not pointing at any address.
How Linked list work is:
the head of the list will point to a address. Then that address will point to another address and so on. Basically, it is like a chain.

So for head to point at anything (in your case starting) it should be passed by reference because you are modifying the head. That's why I did
node **temp = &starting
And then you assign this to a temp2 node. Then check if the node is null.
Condition will be true because in constructor you have specified
starting = NULL;
If the head is null then add the node to the head. And then assign head to this node, hence starting = temp2
Then for the second entry, when the head will not be NULL, you have to iterate through the list till the next of the node is NULL (since in single linked list this is how we mark the end of the list. When the last next is NULL therefore end of list). Then at this next you will create new node, hence
temp2->next = new node;
then change to this node since you have to assign values to this node
hence
temp2 = temp2-next;
Then assign values

Hope this helps
Last edited on
thanks guestgulkan
and kevinchkin

you both are very helpful
Topic archived. No new replies allowed.