c++ - template class + operators + friends = unresolved externals -
i have class called fraction, , i'm declaring operators friends. declared friend operators beforehand, http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16 told me do, fixed +, -, *, , /. << , >> still don't work.
template <class t> class fraction; template <class t> fraction<t> operator+ (fraction<t> const& left, fraction<t> const& right); template <class t> fraction<t> operator- (fraction<t> const& left, fraction<t> const& right); template <class t> fraction<t> operator* (fraction<t> const& left, fraction<t> const& right); template <class t> fraction<t> operator/ (fraction<t> const& left, fraction<t> const& right); template <class t> ostream& operator<< (const ostream& output, fraction<t> const& value); template <class t> istream& operator>> (const ostream& input, fraction<t> const& value);
and class:
template <class t> class fraction { ... friend fraction<t> operator+ <>(const fraction<t>& left, const fraction<t>& right); friend fraction<t> operator- <>(const fraction<t>& left, const fraction<t>& right); friend fraction<t> operator* <>(const fraction<t>& left, const fraction<t>& right); friend fraction<t> operator/ <>(const fraction<t>& left, const fraction<t>& right); friend ostream& operator<< <>(const ostream& output, const fraction<t> value); friend istream& operator>> <>(const istream& input, fraction<t> value); } template <class t> ostream& operator<< <>(const ostream& output, const fraction<t>& value) { output << value.numerator << '/' << value.denominator; return output; } template <class t> istream& operator>> <>(const istream& input, fraction<t>& value) { t n, d, char seperator; cin >> n >> seperator >> d; value.setnumerator(n); value.setdenominator(d); return input; }
as "dark_charlie" says, remove const
.
i'd rather have made comment, unfortunately stackoverflow not yet allow me comment (to person feeling urge comment shouldn't make comment: it's aimed @ you).
Comments
Post a Comment