gpt4 book ai didi

c++ - "ambiguous overload for ' operator<< '"*without* a catch-all overload

转载 作者:行者123 更新时间:2023-11-30 04:04:29 26 4
gpt4 key购买 nike

所以我正在尝试实现 xorshift PRNG 作为来自 random 的参数化 STL 样式类,例如std::mersenne_twister_engine , 所以我可以将它与来自 random 的那些非常方便的发行版一起使用图书馆等

无论如何,我在重载 operator<< 时遇到问题而且,坦率地说,我完全被难住了。

类的参数化如下:

template <size_t __n,
int_least8_t __a, int_least8_t __b, int_least8_t __c,
uint64_t __m>
class xorshift_engine
{
...

重载声明为 friend在类中是这样的:

template <size_t __n_,
int_least8_t __a_, int_least8_t __b_, int_least8_t __c_,
uint64_t __m_,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
const xorshift_engine<__n_, __a_, __b_, __c_, __m_>& _x);

它在类外的实现是这样的:

template <size_t __n,
int_least8_t __a, int_least8_t __b, int_least8_t __c,
uint64_t __m,
typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
const xorshift_engine<__n, __a, __b, __c, __m>& _x)
{
...
}

当我尝试编译以下内容时:

#include <iostream>
#include <random>
#include "xorshift.hpp"

using namespace std;

int main()
{
xorshift1024star bip(2345);
mt19937_64 bip2(2345);
cout << bip << endl;
cout << endl << bip2 << endl;
return 0;
}

( xorshift1024star 只是 xorshift_engine 类的实例:

typedef xorshift_engine<16, -31, 11, 30, 1181783497276652981ULL> xorshift1024star;

) 我收到此错误(我将文件路径中的用户名替换为 --- ):

C:\Users\---\Documents\randgen.cpp: In function 'int main()':
C:\Users\---\Documents\randgen.cpp:11:7: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'xorshift1024star {aka xorshift_engine<16ull, -31, 11, 30, 1181783497276652981ull>}')
cout << bip << endl;
^
C:\Users\---\Documents\randgen.cpp:11:7: note: candidates are:
In file included from C:\Users\---\Documents\randgen.cpp:3:0:
C:\Users\---\Documents\xorshift.hpp:898:1: note: std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const xorshift_engine<__n, __a, __b, __c, __m>&) [with long long unsigned int __n = 16ull; signed char __a = -31; signed char __b = 11; signed char __c = 30; long long unsigned int __m = 1181783497276652981ull; _CharT = char; _Traits = std::char_traits<char>]
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
^
C:\Users\---\Documents\xorshift.hpp:858:2: note: std::basic_istream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const xorshift_engine<__n_, __a_, __b_, __c_, __m_>&) [with long long unsigned int __n_ = 16ull; signed char __a_ = -31; signed char __b_ = 11; signed char __c_ = 30; long long unsigned int __m_ = 1181783497276652981ull; _CharT = char; _Traits = std::char_traits<char>; long long unsigned int __n = 16ull; signed char __a = -31; signed char __b = 11; signed char __c = 30; long long unsigned int __m = 1181783497276652981ull]
operator<< (std::basic_ostream<_CharT, _Traits>& os,
^
In file included from C:/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/c++/iostream:39:0,
from C:\Users\---\Documents\randgen.cpp:1:
C:/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/c++/ostream:602:5: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = xorshift_engine<16ull, -31, 11, 30, 1181783497276652981ull>] <near match>
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
C:/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/c++/ostream:602:5: note: no known conversion for argument 1 from 'std::ostream {aka std::basic_ostream<char>}' to 'std::basic_ostream<char>&&'

作为引用,这是在 mersenne_twister_engine 中声明重载的方式在bits/random.h :

template<typename _UIntType1,
size_t __w1, size_t __n1,
size_t __m1, size_t __r1,
_UIntType1 __a1, size_t __u1,
_UIntType1 __d1, size_t __s1,
_UIntType1 __b1, size_t __t1,
_UIntType1 __c1, size_t __l1, _UIntType1 __f1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
__m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
__l1, __f1>& __x);

及其在 bits/random.tcc 中的实现:

template<typename _UIntType, size_t __w,
size_t __n, size_t __m, size_t __r,
_UIntType __a, size_t __u, _UIntType __d, size_t __s,
_UIntType __b, size_t __t, _UIntType __c, size_t __l,
_UIntType __f, typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const mersenne_twister_engine<_UIntType, __w, __n, __m,
__r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
{
...
}

如果我注释掉 cout << bip << endl;在我的测试代码中,它编译并运行没有错误,所以这个重载没问题,但我的不行。

我完全没有想法。我错过了什么?如果有人能帮助我,我将不胜感激。


编辑:对于那些建议我应该通过前向声明函数来解决的“模板 friend ”问题的人,我刚刚添加了

template <size_t __n,
int_least8_t __a, int_least8_t __b, int_least8_t __c,
uint64_t __m>
class xorshift_engine;

template <size_t __n,
int_least8_t __a, int_least8_t __b, int_least8_t __c,
uint64_t __m,
typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
const xorshift_engine<__n, __a, __b, __c, __m>& _x);

在类主体/实现之前,我在尝试编译时遇到了同样的错误。


edit2:给出相同编译错误的简化示例:

//file "failclass.hpp"

#ifndef _FAILCLASS_HPP
#define _FAILCLASS_HPP

#include <iosfwd>
#include <type_traits>

template <int n>
class failclass
{
private:
static constexpr int k = n;

public:
template<int n1, typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
const failclass<n1>& _x);
};

template<int n1, typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
const failclass<n1>& _x)
{
_os << _x.k;
return _os;
}

#endif // _FAILCLASS_HPP

尝试编译这个:

//file "failtest.cpp"

#include <iostream>
#include "failclass.hpp"

using namespace std;

int main()
{
failclass<5> a;
cout << a;
return 0;
}

最佳答案

留下下面答案的其余部分作为其值(value),但特定错误只是一个拼写错误:

template <size_t __n_,
int_least8_t __a_, int_least8_t __b_, int_least8_t __c_,
uint64_t __m_,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
// ^ -- you probably meant std::ostream!!!!!
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
const xorshift_engine<__n_, __a_, __b_, __c_, __m_>& _x);

在命名空间级别定义的模板和友元采用完全相同的参数,但具有不同的返回类型。


以下大部分内容并不是对您的错误消息的直接回答,而是解决了让您陷入困境的根本问题。我个人认为前向声明应该已经修复了它。如果没有,您应该提供 SCCE。

为了便于讨论,让我们将代码稍微简化为一个模板,其中包含您要为其实现 operator<< 的单个参数。 .好友声明:

template <typename T>
class Tmpl {
friend std::ostream& operator<<(std::ostream&, Tmpl const &);
};

非模板独立函数提供声明operator<<服用std::ostream&和一个 Tmpl<T> const & .这里有一个重要的细节,这不是给模板加好友,也不是任何免费功能。鉴于专业 Tmpl<int> ,该声明与具有以下签名的同一命名空间中的函数成为 friend :

std::ostream& operator<<(std::ostream& out, Tmpl<int> const & obj) {
// I can access Tmpl<int> internals!
return out;
}

虽然这是一种可能的方法,但您很可能不想为模板的每个特化手动提供不同的免费函数。

此时 friend 有一个非常有趣的特征声明:您可以提供类内部的定义,连同友元声明:

template <typename T>
class Tmpl {
friend std::ostream& operator<<(std::ostream out, Tmpl const & t) {
// definition goes here
return out;
}
};

这里有趣的一点是,对于 Tmpl 的每个特化,编译器将为您生成一个非模板自由函数,该函数可以访问该类型的内部结构。

现在,在这种特殊情况下,您可能需要考虑其他替代方案。我经常使用的第一个想到的是不要制作 operator<<一个 friend ,但提供一个print函数(您可以向其添加其他参数来控制输出),然后实现 operator<<在公共(public)接口(interface)方面调用 print .您可以选择仍然制作 operator<<在类 [1] 中定义的 friend ,或者您可以提供模板化的 operator<<那叫print :

template <typename T>
class Tmpl {
public:
std::ostream& print(std::ostream& out) const;
// option 1:
friend std::ostream& operator<<(std::ostream& out, Tmpl const & obj) {
return obj.print(out);
}
};
// option 2:
template <typename T>
std::ostream& operator<<(std::ostream& out, Tmpl<T> const & obj) {
return obj.print(out);
}

另一种选择(我不推荐,但为了完整起见)是在 friend 之上声明模板:

template <typename T>
class Tmpl {
public:
template <typename U>
friend std::ostream& operator<<(std::ostream& out, Tmpl<U> const & obj);
};
// template defined as option 2 above

但这是一个的想法(以及您选择的替代方案),如operator<< <int>将可以访问 Tmpl<double> , 并且很容易破坏封装。

一个稍微好一点的选择是与上述模板的特化完全匹配参数,但这在代码中有点复杂:

template <typename T> class Tmpl;
template <typename T>
std::ostream& operator<<(std::ostream&, Tmpl<T> const &);
template <typename T>
class Tmpl {
friend std::ostream& operator<< <T>(std::ostream&, Tmpl<T> const &);
};

1 仍然生成operator<<的可能原因一个 friend ,即使它仅根据公共(public)接口(interface)实现,它也隐藏正常查找的运算符,并使其仅可用于 ADL。

关于c++ - "ambiguous overload for ' operator<< '"*without* a catch-all overload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com