Thread: problems overloading outstream for custom ADT

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    problems overloading outstream for custom ADT

    Hi, I am doing a project where i make my own ADT, in this case a chemical compound. I am having problems overloading the << operator as a friend function and still being able to access private class variables. Any help would be appreciated, here is the code:

    Compound.h
    Code:
    #include <iostream>
    #include <string>
      
    using namespace std;
      
    class Compound{
      
      public:
             //constructors and accessors here
    
       friend ostream & operator<< ( ostream & os, const Compound & source );
      friend istream & operator>> ( istream & is, Compound & target );
      
     private:
               string name, ion;
               int protons, neutrons, electrons, weight, charge;
    };
      
    ostream & operator<< ( ostream & os, const Compound & source );
    istream & operator>> ( istream & is, const Compound & target );

    Compound.cc
    Code:
    #include "Compound.h"
    #include <string>
      
    using namespace std;
    
    //all accessors/constructors done here
    
    ostream & operator<< ( ostream & os, const Compound & source )
    {
      os << source.name;
      return os;
    }
     
    istream & operator>> ( istream & is, const Compound & target )
    {
      is >> target.name;
      return is;
    }
    The error
    Code:
    Compound.h: In function `std::istream& operator>>(std::istream&, const Compound&)':
    Compound.h:44: error: `std::string Compound::name' is private
    Compound.cc:147: error: within this context

    Also, as you can see in my .h file i did 'using namespace std;'
    My prof said not to use this but when I remove it nothing seems to work even though I also use it in the *.cc implementation file, mainly it says 'strings' arent a type even tho I #include <string>. Any suggestions here would be helpful as well. If more code is needed to see the problem let me know, thanks.
    -alex

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Try get ridding of the second declaration of << and >> that you have after you class. You already delcared them as friend in your class. That is enough.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    Okay I tried removing the second 2 declarations of << and >> but I am still getting the same error. Would it have anything to do with the scope of namespace std? Everywhere I look it says I should stay away from using namespace std in header files but my strings won't work without it, could this also be part of my private variable problem?

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Read carefully:
    Code:
    //declaration
    friend ostream & operator<< ( ostream & os, const Compound & source );
    friend istream & operator>> ( istream & is, Compound & target );
    
    //implementation
    ostream & operator<< ( ostream & os, const Compound & source )
    istream & operator>> ( istream & is, const Compound & target )
    Either remove const from the operator>> implementation paramater or add const to the operator>> declaration parameter.

    edit:
    Also, I would remove the "using namespace std;" and instead fully qualify the parts of the std namespace that you use. Example:
    Code:
    Compound.h
    #include <iostream>
    #include <string>
      
    class Compound{
      
      public:
             //constructors and accessors here
    
       friend std::ostream & operator<< ( std::ostream & os, const Compound & source );
      friend std::istream & operator>> ( std::istream & is, Compound & target );
      
     private:
               std::string name, ion;
               int protons, neutrons, electrons, weight, charge;
    };
      
    std::ostream & operator<< ( std::ostream & os, const Compound & source );
    std::istream & operator>> ( std::istream & is, const Compound & target );
    Last edited by pianorain; 04-05-2005 at 08:51 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    ... remove const from the operator>> ...
    Do this, you don't want your variable to be const, if you make it const you won't be able to input a value into it.

    I would still remove, the second declarations, they aren't needed, however if they match (parameter and type) then they shouldn't cause any problems being there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  2. problems overloading =
    By Mr_Jack in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2004, 09:19 PM
  3. New to board problems with overloading
    By Turbo02 in forum C++ Programming
    Replies: 21
    Last Post: 12-18-2002, 07:38 PM
  4. Problems: Operator overloading.
    By Dual-Catfish in forum C++ Programming
    Replies: 17
    Last Post: 06-18-2002, 06:38 PM
  5. problems with >> istream overloading
    By rip1968 in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2002, 03:38 PM