istream >> overloading

This is a discussion on istream >> overloading within the C++ Programming forums, part of the General Programming Boards category; I am using visual c++ and I am having trouble overloading the >> operator. When I compile it says that ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    8

    Unhappy istream >> overloading

    I am using visual c++ and I am having trouble overloading the >> operator. When I compile it says that _simNo is private and it can't be accesed. Why doesn't this work:

    SimCard.h

    #include <string>
    #include <iostream>

    using namespace std;

    class SimCard
    {
    public:
    SimCard(const string& tempSimNo = "", const string& tempCustName = "", const string& tempAddress = "", const string& tempSuburb = "", const float& tempCredit = 0.0);
    friend istream &operator>> (istream& is, SimCard& sim);
    void setCredit(const float credit);
    string getSimNo() const;
    ~SimCard();

    private:
    string _simNo;
    string _custName;
    string _address;
    string _suburb;
    float _credit;
    };

    SimCard.cpp

    #include "SimCard.h"

    using namespace std;

    SimCard::SimCard(const string& tempSimNo, const string& tempCustName, const string& tempAddress, const string& tempSuburb, const float& tempCredit)
    {
    _simNo = tempSimNo;
    _custName = tempCustName;
    _address = tempAddress;
    _suburb = tempSuburb;
    _credit = tempCredit;
    }

    istream& operator>> (istream& is, SimCard& sim)
    {
    getline(is, sim._simNo);
    getline(is, sim._custName);
    getline(is, sim._address);
    getline(is, sim._suburb);
    is>>sim._credit;
    is.ignore();
    return is;
    }

    void SimCard::setCredit(const float credit)
    {
    _credit = credit;
    }

    string SimCard::getSimNo() const
    {
    return _simNo;
    }

    SimCard::~SimCard()
    {

    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I don't know.. it worked fine for me.. compiled with -pedantic flag (strictest) option in gcc.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:17 AM
  2. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 05:33 PM
  3. Compile errors with overloading >> operators
    By GMHummerH1 in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2004, 07:13 PM
  4. << and >> overloading
    By Armatura in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2003, 06:19 PM
  5. problems with >> istream overloading
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2002, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21