模板类 +运营商 +朋友=未解决的外部因素

发布于 2024-09-26 17:47:00 字数 2307 浏览 0 评论 0原文

我有一个名为分数的类,并且我将一些运算符声明为朋友。我事先声明了友元运算符,如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-10-03 17:47:00

正如“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).

自由如风 2024-10-03 17:47:00

替换

template <class T> istream& operator>> <>(const istream& input, Fraction<T>& value)  { ...

template <class T> istream& operator>> (const istream& input, Fraction<T>& value)  { ...

对于其他运算符, And 类似。问题是,通过这种方式,您将创建一个空白模板专业化,它会产生与向前声明为朋友的符号不同的符号。

编辑:

我看到的另一个问题是这个前向声明:

template <class T> istream& operator>> (const ostream& input, Fraction<T> const& value);

您将其第一个参数声明为const ostream&,它应该是const istream&。这肯定会导致链接器错误。

旁注:原始答案不应根据常见问题解答修复错误。但是,如果问题仍然存在,我会尝试一下。

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&. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文