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
The errorCode:#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; }
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



LinkBack URL
About LinkBacks


