[英]template class + operators + friends = unresolved externals
I have a class called fraction, and I'm declaring some operators as friends. 我有一个叫做“分数”的类,我在宣布一些运算符为朋友。 I declared the friend operators beforehand, as http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16 told me to do, but it only fixed +, -, *, and /.
我事先声明了朋友运算符,因为http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16告诉我要做,但它仅固定+,-,*和/。 << and >> 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 the 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, but remove the const
. 就像“ dark_charlie”所说,但是删除
const
。
I'd rather have made this as just a comment, but unfortunately StackOverflow does not yet allow me to comment (to the person feeling the urge to comment that I shouldn't make this comment: it's aimed at you ). 我宁愿只是作为评论而已,但不幸的是,StackOverflow尚未允许我发表评论(对那些渴望发表评论的人,我不应该发表评论:这是针对您的 )。
Replace 更换
template <class T> istream& operator>> <>(const istream& input, Fraction<T>& value) { ...
with 与
template <class T> istream& operator>> (const istream& input, Fraction<T>& value) { ...
And analogically for the other operator. 类似地,其他操作员。 The problem is that this way you are creating a blank template specialization which yields a different symbol than the one being forward-declared as friend.
问题在于,通过这种方式您正在创建空白模板特化,该特化所生成的符号与被向前声明为朋友的符号不同。
EDIT:
编辑:
Another problem I see is this forward declaration: 我看到的另一个问题是此前向声明:
template <class T> istream& operator>> (const ostream& input, Fraction<T> const& value);
You declare its first parameter as const ostream&
, it should be const istream&
. 您将其第一个参数声明为
const ostream&
,应为const istream&
。 This definitely will cause a linker error. 这肯定会导致链接器错误。
Side note: The original answer should not fix the errors according to the FAQ. 旁注:根据常见问题解答,原始答案不能解决错误。 However, I'd give it a shot if the problem persisted.
但是,如果问题仍然存在,我会试一试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.