Thread: cin, inheeritance

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    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.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    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?

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    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;
    }
    Last edited by guda; 09-27-2004 at 10:02 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    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.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. Problem Calling Destructor, and prob with cin.
    By imortal in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2003, 09:29 AM