Structs with a Class

Hello,

I keep getting the following error when trying to use structs within a class (see code below):

"invalid use of 'struct node::person' "

Any assistance will be appreciated.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# include <iostream>
# include <fstream>

using namespace std;

class node
{
 
      public:
            
            struct person
            {
             char FNAME[ 50 ];
             char LNAME[ 50 ];
             char ADDRESS[ 50 ];
             node *link;      
             };
             
             struct animal
             {
                    
                    
                    
                    
             };
      
};

int main()
{
    
 char fname[ 50 ];
 char lname[ 50 ];
 char address[ 50 ];   
    
 ofstream outfile;
 outfile.open("Delete.txt",ios::app);
 outfile.close();
 

 ofstream outflle;
 outfile.open("Delete.txt",ios::app);
 
 cout << "Enter your first name: ";
 cin.getline(fname, 50);
 
 
 cout << "Enter your last name: ";
 cin.getline(lname, 50);
 
 
 cout << endl << "ENter your address: ";
 cin.getline(address, 50);
 
 
 outfile << fname << " " <<  lname << endl << address << endl << endl;
 
 outfile.close();
 
 
 
 
 node *head, *first, *newNode, *last;
 
 
 head = NULL;
 first = NULL;
 newNode = NULL;
 last = NULL;
 
 ifstream infile;
 infile.open("Delete.txt");
 
 
 while ( !infile.fail() )
 {
 
  
 newNode = new node;
 
 infile >> newNode->person->FNAME >> newNode->person->LNAME;
 infile >> newNode->person->ADDRESS;
 
 newNode->person->link = NULL;
 
 if ( first == NULL )
 {
 
  first = newNode;
  head = newNode;
  last = newNode;     
      
 }
 else
 {
  
  last->link = newNode;
  last = newNode;
           
 }
 
 
 
}// end while 
    
    infile.close();
    
    
    
    
    
    
    
    newNode = head;
    
    while( first->link != NULL )
    {
           
           cout << first->FNAME   << " " << first->LNAME << endl
                << first->ADDRESS << endl << endl;
                
    
           first = first->link;
           
           
           
    }// end while
    
    
    
    
    system ("pause");
}
If I'm thinking correctly, you are referring to the struct type as if it was a variable. If you want, you could do this instead:
1
2
3
4
5
6
7
struct //create an anonymous struct
{
  char FNAME[ 50 ];
  char LNAME[ 50 ];
  char ADDRESS[ 50 ];
  node *link;      
} person; //the variable name is 'person' 


nodeName->person refers to the struct
nodeName->person.member refers to a member of the struct.

Using that information, you should be able to fix your program.

I hope this helps! ^_^

rpgfan
rpgfan3233,
Thank you very much for your reply.

Sorry for not posting this code: this is what I was trying to do:

1
2
infile >> newNode->person->FNAME >> newNode->person->LNAME;
 infile >> newNode->person->ADDRESS;


I was trying to read the file and store the contents using the unique variables stored in struct person from within the class but I kept getting that error.

Thanks for your solution, I am currently trying to get it to work, by the way, is this struct declared inside the class or outside? Best regards and thanks again
Last edited on
If you're using it as newNode->person->FNAME, it would be declared inside the class. Also, if you're using person->FNAME and stuff, you should really be using '*person' rather than just 'person'. After all, -> refers to the idea of pointing to a member of a struct/class. If person isn't a pointer, how can it point to anything? :P
Topic archived. No new replies allowed.