-
cin, inheeritance
HI,
Im trying to get this to work, however i cannot and it is driving me crazy, what I have is a text file from which i am trying to read in the data into members of a base class, and two derived classes:
Base class:
Code:
class insurance: public date {
private:
string firstName;
string lastName;
char gender;
date birthDate;
protected:
int total;
};
Derived class 1:
Code:
class car : public insurance {
private:
std::string manufacturer;
std::string model;
int year;
std::string color;
int size;
public:
car();
car(std::string, std::string, int, std::string, int);
Derived class II:
Code:
class health : public insurance {
private:
int type;
int coverage;
int size;
public:
health();
health (int,int,int);
main.cpp
Code:
int MAX_SIZE = 100;
car c[MAX_SIZE];
health h[MAX_SIZE];
while(inFile) {
test types
inFile >> type;
if(type == 1) {
inFile >> c[i];
i++;
}
else if(type == 2) {
inFile >> h[i];
i++;
}
What I am trying to do is read in lines of a file....
formatted like this:
Code:
2 James Williams M 3 12 1980 1 1
I have tried overloading >> eg.. for the car class using the following function
Code:
istream operator >>(istream &in, car &c) {
in >> firstName >> lastName >> gender >> birthDate
>> manufacturer >> model >> year >> color;
// return in;
}
however I cannot seem to get it to work, if anyone has any ides, or sees anything im doing wrong please let me know, thanks in advance for any assistance.
-
Are you getting any errors at either compile or run-time? If so, please include them.
What is the program actually doing if it is working? I can't see where you are opening the file to read from, but I am sure you are openeing it successfully before calling the function?
-
I get various errors... , and yes I am successfully opening the file..
it starts out with something like this:
Code:
g++ -g -Wall -c car.cxx
car.cxx: In function `std::istream operator>>(std::istream&, car&)':
car.cxx:20: error: no match for 'operator>>' in 'operator>>(std::istream&,
date&)(((+c) + 24)) >> c->car::manufacturer'
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/istream.tcc:86: error: 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/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/istream.tcc:92: error:
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>]
I assume this error means that I have not or am not using the overloaded operator correctly, or have not included it in my class properly?
With a slightly modified operator function defined in car.cpp:
Code:
istream operator >>(istream &in, car &c) {
in >> c.firstName >> c.lastName >> c.gender >> c.birthDate
>> c.manufacturer >> c.model >> c.year >> c.color;
// return in;
}
-
Well, I've never overloaded >> myself, but I will say that it shuld be returning something... and your return is commented.
Another thing to try is to try doing it one at a time instead of in a large listing.
-
Make sure you included <string>. It looks like it cannot find the operator>> for the string class. I assume that your operator>> is a friend, because it is accessing private member variables. If not, it should be. Also, you should return istream& instead of istream, although that shouldn't be affecting your program right now.