模板类 +运营商 +朋友=未解决的外部因素
我有一个名为分数的类,并且我将一些运算符声明为朋友。我事先声明了友元运算符,如 http://www.parashift .com/c++-faq-lite/templates.html#faq-35.16告诉我这样做,但它只修复了+、-、*和/。 <<和>>还是不行。
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);
还有班级:
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;
}
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 /. << 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如“dark_charlie”所说,但删除
const
。我宁愿将其仅作为评论,但不幸的是 StackOverflow 还不允许我发表评论(对那些想要发表评论的人来说,我不应该发表此评论:它是针对你)。
As "dark_charlie" says, but remove the
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).
替换
为
对于其他运算符, And 类似。问题是,通过这种方式,您将创建一个空白模板专业化,它会产生与向前声明为朋友的符号不同的符号。
编辑:
我看到的另一个问题是这个前向声明:
您将其第一个参数声明为
const ostream&
,它应该是const istream&
。这肯定会导致链接器错误。旁注:原始答案不应根据常见问题解答修复错误。但是,如果问题仍然存在,我会尝试一下。
Replace
with
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:
You declare its first parameter as
const ostream&
, it should beconst 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.