c++ fraction class. overloading operators? -
i making fraction class school project, , brain frying. told overload << , >> operators through friend keyword. i'm getting errors this.
i've posted relevant code here: http://pastebin.com/ngcabgj2
the errors include: error c2270: '<<' : modifiers not allowed on nonmember functions (this error ones declared friends)
this @ operator< definition. error c2333: 'fraction::operator <' : error in function declaration; skipping function body
there's 46 in all... nightmare.
edit:
thanks, solved errors, there's still 3
error c2664: 'fraction::fraction(const fraction &)' : cannot convert parameter 1 'int' 'const fraction &' occurs @ statement:
fraction<int> test1, test2, test3(10);
error c2248: 'fraction::operator ==' : cannot access private member declared in class 'fraction' error c2248: 'fraction::operator <' : cannot access private member declared in class 'fraction'
i don't understand these two, occurs @ these statements:
if (test1 == test2) cout << "\ntest1 equal test2"; if (test1 < test2) cout << "\ntest1 less test2";
thanks!
<><><>>edit2<<><><>
i fixed other private access errors, have reaaaaaaaally bizarre errors:
full code: http://pastebin.com/mvrb67sr
errors:
error 1 error lnk2001: unresolved external symbol "class fraction __cdecl operator-(class fraction const &,class fraction const &)" (??g@ya?av?$fraction@h@@abv0@0@z) error 2 error lnk2001: unresolved external symbol "class fraction __cdecl operator+(class fraction const &,class fraction const &)" (??h@ya?av?$fraction@h@@abv0@0@z) error 3 error lnk2001: unresolved external symbol "class fraction __cdecl operator/(class fraction const &,class fraction const &)" (??k@ya?av?$fraction@h@@abv0@0@z) c:\users\caleb jares\documents\visual studio 2010\projects\solution11-5\solution11-5\solution11-5.obj error 4 error lnk2001: unresolved external symbol "class fraction __cdecl operator*(class fraction const &,class fraction const &)" (??d@ya?av?$fraction@h@@abv0@0@z) error 5 error lnk2001: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > const &,class fraction)" (??6@yaaav?$basic_ostream@du?$char_traits@d@std@@@std@@abv01@v?$fraction@h@@@z) error 6 error lnk2001: unresolved external symbol "class std::basic_istream > & __cdecl operator>>(class std::basic_istream > const &,class fraction)" (??5@yaaav?$basic_istream@du?$char_traits@d@std@@@std@@abv01@v?$fraction@h@@@z) error 7 error lnk1120: 6 unresolved externals
again, help!
sounds tried declare friend ostream &operator<<(…) const;
. important thing friend
s are not members. friend
function exists outside scope of class, if defined inside class {}
block. in other words, declaring function ::operator<<()
, not fraction::operator<<()
. , member functions can have trailing const
, since modifies type of this
.
fact is, operator<<
output shouldn't friend anyway. gets value , forwards stream… shouldn't require special permission! same applies operator<
.
take functions outside class block entirely. there's no way ta can complain design using friend
less often.
Comments
Post a Comment