Help me make sense of these errors dealing with Classes ?

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
 carLot.cpp: In function `int main()':
carLot.cpp:155: no match for `std::ifstream& >> AUTO&' operator
/usr/local/include/c++/3.2.2/bits/istream.tcc:83: candidates are: 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(std::basic_istream<_CharT, 
   _Traits>&(*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, 
   _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:92:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(std::basic_ios<_CharT, 
   _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits 
   = std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:101:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, 
   _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:110:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(bool&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:137:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(short int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:174:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:201:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:238:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:265:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(long int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:292:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:320:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:347:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:375:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(float&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:402:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(double&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:429:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(long double&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:456:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(void*&) [with _CharT = char, _Traits = 
   std::char_traits<char>]
/usr/local/include/c++/3.2.2/bits/istream.tcc:483:                 
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 
   _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = 
   char, _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/istream:236:                 
   std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, 
   _Traits>&, unsigned char&) [with _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/istream:241:                 
   std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, 
   _Traits>&, signed char&) [with _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/istream:250:                 
   std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, 
   _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/istream:255:                 
   std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, 
   _Traits>&, signed char*) [with _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/iomanip:63:                 
   std::basic_istream<_CharT, _Traits>& 
   std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Resetiosflags) 
   [with _CharT = char, _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/iomanip:90:                 
   std::basic_istream<_CharT, _Traits>& 
   std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setiosflags) 
   [with _CharT = char, _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/iomanip:117:                 
   std::basic_istream<_CharT, _Traits>& 
   std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setbase) [with 
   _CharT = char, _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/iomanip:179:                 
   std::basic_istream<_CharT, _Traits>& 
   std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setprecision) 
   [with _CharT = char, _Traits = std::char_traits<char>]
/usr/local/include/c++/3.2.2/iomanip:206:                 
   std::basic_istream<_CharT, _Traits>& 
   std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setw) [with 
   _CharT = char, _Traits = std::char_traits<char>]

                

 

my 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
 // --------------------------------------------------------------------
// Author:      bfields Byron Fields
// Assignment:  PROG4c
//
// File name:   carLot.cpp 
//
// Purpose:     Manage carlot with initial 10-car inventory, 3 sales.
//
// Due Date:    2/22/2013 at 11pm 
// 
// --------------------------------------------------------------------

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class AUTO
{
 private:
   string Make;   // Manufacturer, e.g., Toyoto.
   string Model;  // Model, e.g., Corolla
   float Price;   // Purchase price, in dollars.
   float MPG;     // Fuel economy.

 public:

   AUTO () {Make = "TBD"; Model = "TBD"; Price = 0.0; MPG = 0.0; }
   
   AUTO (string make, string model, float cost, float mpg)
   {
      Make = make; Model = model; Price = cost; MPG = mpg;
   }

   void Set_Cost(float cost) {Price = cost;}
   void Set_MPG(float mpg) {MPG = mpg;}
   void Set_Make(string make) {Make = make;}
   void Set_Model(string model) {Model = model;}

   // Read ONE car from KEYBOARD input.
   void ReadData() 
   { 
      cout << endl << "Enter Price, Make, Model and MPG for car: ";
      string Make1;   
      string Model1;  
      float Price1;   
      float MPG1;
      cin >> Price1 >> Make1 >> Model1 >> MPG1;

      Set_Cost(Price1);
      Set_MPG(MPG1);
      Set_Make(Make1);
      Set_Model(Model1);

  
   }

   // Read ONE car from input file stream (already open).
   void ReadData(istream & invF) 
   { 

   cout << endl << "Enter Price, Make, Model and MPG for car: ";
      string Make1;   
      string Model1;  
      float Price1;   
      float MPG1;
      invF >> Price1 >> Make1 >> Model1 >> MPG1;

      Set_Cost(Price1);
      Set_MPG(MPG1);
      Set_Make(Make1);
      Set_Model(Model1);
      
   }

   float getPrice()
   {
   return Price;   
   }

   float getMPG()
   {
   return MPG;     
   }

   string getMake()
   {
    return Make;    
   }

   string getModel ()
   {
   return Model;   
   }

   // Display formatted car record to CONSOLE.
   void Show()
   {
      cout << endl << "CAR: " << Make << " " << Model << " cost $ " 
           << setprecision(2) << fixed << Price << " and gets " << MPG 
           << " mpg" << endl;
   }
}; // class AUTO 

int main()
{

   // -----------------------------------------------------------------------------
   // Declare 20 AUTO objects using default constructor.
   // -----------------------------------------------------------------------------
   AUTO  Inventory[20];    // Inventory of up to 20 cars. 
   int invSize=0;          // #cars in inventorylo.
   int numAdded=0;         // #cars added to inventory.

   // -----------------------------------------------------------------------------
   // Declare other variables needed by program.
   // -----------------------------------------------------------------------------
   ifstream invF;    // Stream attached to inventory file.
   int k;            // Loop variable.
   float sumPrice;   // Sum of cost of cars in inventory.
   float sumMPG;     // Sum of the gas mileage of cars in inventory.
   float avgPrice;   // Average cost of cars.
   float avgMPG;     // Average gas mileage of cars.


   //-| --------------------------------------------------
   //-| Print the copyright notice declaring authorship.
   //-| --------------------------------------------------
   cout << endl << endl << "(c) 2013, bfields Byron Fields" << endl;


   //-| ---------------------------------------------------------------------
   //-| 1. Exit program if input file not found.
   //-| ---------------------------------------------------------------------
   invF.open("inventory.dat");
   if (invF.fail())
   {
      cout << endl << "INVENTORY FILE 'inventory.dat' NOT FOUND ... TERMINATING."
           << endl;
      exit(1); 
   }


   //-| ---------------------------------------------------------------------
   //-| 2. Read in size of starting inventory.
   //-| ---------------------------------------------------------------------
   cout << "Enter #cars at start of day: " << endl;
   cin >> invSize;

   //-| ---------------------------------------------------------------------
   //-| 3. Load 'size' inventory records from inventory.dat file.
   //-| ---------------------------------------------------------------------
   for (k=0; k<invSize; k++)
   {
   invF >> Inventory[k];
   }

   //-| ---------------------------------------------------------------------
   //-| 4. Calculate the average price of the cars in the inventory.
   //-| ---------------------------------------------------------------------
   sumPrice = 0;
   for (k=0; k<invSize; k++)
   {
   sumPrice += Inventory[k].getPrice();   
   }
   if (invSize > 0)
      avgPrice = sumPrice / invSize;
   else 
      avgPrice = 0.00;
   
   cout << endl << invSize << " CARS ON LOT (Start of Business)" << endl;


   //-| ---------------------------------------------------------------------
   //-| 5. Calculate the average MPG of the cars in the inventory.
   //-| ---------------------------------------------------------------------
   sumMPG = 0;
   for (k=0; k<invSize; k++)
   {
   sumMPG += Inventory[k].getMPG();   
   }
   if (invSize > 0)
      avgMPG = sumMPG / invSize;
   else 
      avgMPG = 0.00;

   cout << endl << "AVERAGE FUEL ECONOMY = " 
        << avgMPG << " MPG" << endl;


   //-| ---------------------------------------------------------------------
   //-| 6. Display the Start-of-Day inventory.
   //-| ---------------------------------------------------------------------
   cout << endl << "CHEAPO CAR LOT INVENTORY (starting)" << endl << endl;
   cout << left << setw(12) << "MAKE" << " " << setw(12) << "MODEL" << " " 
        << setw(10) << right << "PRICE ($)" << " " << setw(6) << "MPG" << endl;
   cout << setfill('-') << left << setw(12) << "-" << " " << setw(12) << "-" 
        << " " << setw(10) << right << "-" << " " << setw(6) << "-" << endl;
   cout << setfill(' ') << fixed;

   for (k=0; k<invSize; k++)
   {
    
    Inventory[k].Show();
    cout << endl; 

       
   }
  


   //-| ---------------------------------------------------------------------
   //-| 7. Read in number of cars being added to inventory. 
   //-| ---------------------------------------------------------------------
   cout << "Enter #cars to be added to inventory: ";
   cin >> numAdded;

   //-| ---------------------------------------------------------------------
   //-| 8. Load numAdded car records into the inventory FROM KEYBOARD.
   //-| ---------------------------------------------------------------------
   for (k=invSize; k<numAdded; k++)
   {
   
   Inventory[k].ReadData(); 

       
   }

   //-| ---------------------------------------------------------------------
   //-| 9. Update the size of the inventory by the #cars added.
   //-| ---------------------------------------------------------------------
   invSize += numAdded;


   //-| ---------------------------------------------------------------------
   //-| 10. Calculate the average price of the cars in the inventory.
   //-| ---------------------------------------------------------------------
        
   sumPrice = 0;
   for (k=0; k<invSize; k++)
   {
   sumPrice += Inventory[k].getPrice();   
   }
   if (invSize > 0)
      avgPrice = sumPrice / invSize;
   else 
      avgPrice = 0.00;
   
   cout << endl << invSize << " CARS ON LOT (Start of Business)" << endl;

   //-| ---------------------------------------------------------------------
   //-| 11. Calculate the average MPG of the cars in the inventory.
   //-| ---------------------------------------------------------------------
        
   sumMPG = 0;
   for (k=0; k<invSize; k++)
   {
   sumMPG += Inventory[k].getMPG();   
   }
   if (invSize > 0)
      avgMPG = sumMPG / invSize;
   else 
      avgMPG = 0.00;

   return 0;
}
You're using strings, but you haven't #included <string>.


If you #include <string> most of those errors will go away. The only one that persists is due to line 155:

invF >> Inventory[k];

That error is because you did not overload the >> operator for your AUTO class.
Two errors that I see:
1) You need a #include <string> .
2) Line 155 - Your AUTO class does not override the >> operator.


thanks all that helped.
I forgot I had written a function in my class to read data from the input file so i just needed to change the code to:

1
2
3
4
5
6
7
//-| ---------------------------------------------------------------------
   //-| 3. Load 'size' inventory records from inventory.dat file.
   //-| ---------------------------------------------------------------------
   for (k=0; k<invSize; k++)
   {
   Inventory[k].ReadData(invF);
   }


and about the #include <string>

what exactly does that entail? because my professor hasnt mentioned it yet.. we just got to pointers in c++
string is a header file which defines the string class and some support functions for it (like the >> and << operators). Without including it, the compiler does not know what a string is.

Just like how you have to #include <iostream> before you can use cin or cout.... you also have to #include <string> before you can use string.
Topic archived. No new replies allowed.