- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在尝试实现 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/
为什么我的重载成员函数只是“不明确”为 char 而不是 int 和 string? 我试图通过重载的 equals() 函数汇集代码,为我的 Char 类创建一个单代码路径。当我使用 equals
我阅读了以下重要问题:Attributes in C#并学到了很多关于属性的知识。 我正在使用一个使用多个属性的库。示例: [State] public class USStates { [C
令我惊讶的是,以下代码在 VS2005 中编译时没有出现任何问题,因为在实现中对 Bar() 的调用让我停下来想知道如何消除歧义。 class Foo { public: void Bar(int
考虑以下几点: struct A { A(int, int) { } }; struct B { B(A ) { } // (1) expl
假设我们有这段代码,是从一个单独的问题中复制过来的: namespace x { void f() { } class C { void f()
考虑 C 代码 a = a = a。没有用于分配的序列点,因此此代码在编译时会产生有关 a 上未定义操作的警告。 a 在这里可能有哪些值? a 似乎无法更改值。这里实际上有未定义的行为还是编译器只是懒
题目地址:https://leetcode.com/problems/ambiguous-coordinates/description/ 题目描述: Wehad some 2-dimension
这个问题在这里已经有了答案: ORA-00918: column ambiguously defined in SELECT * (4 个答案) 关闭 5 年前。 所以我这学期很难理解 SQL。我真
我对 git 还很陌生。目前我尝试按照本教程使用分支名称和版本覆盖我的应用程序的图标:http://www.merowing.info/2013/03/overlaying-application-v
我已经升级到Xcode 11和Swift 5,并且在通过框架提供方法扩展时遇到了一个问题。更具体地说,在一个结构如下的项目中: -> Main Project -> Framework created
我有这样的片段: template bool apply_impl(data_t * d) const { return this->Last::apply(*
以下数据库结构: 表 cms_pages 包含带有 id、名称、标题等的页面 表 cms_pagerows 包含具有 cms_page_id 和排名的行 (cms_page_id 上的唯一索引,排名)
我正在尝试使用以下代码创建一个客户列表以及他们购买的品牌。 brands 表包含品牌名称,customer_id 在 customers 表中。要链接它们,我必须通过 receipts 表(连接到 c
我收到错误 “Integrity constraint violation: 1052 Column 'restaurant_id' in where clause is ambiguous' in”
我有一个在页面中间有一个表格 View 的布局。我希望根据用户设备的屏幕尺寸任意调整表格 View 的大小。在 ascii 中: +-----------+ |some stuff | +------
我正在尝试创建一个可以帮助您计算商品销售税的应用程序。当然应用程序需要乘法但我一直遇到错误: "Type of expression is ambiguous without more context
我正在尝试使用以下类型别名来定义它来传递一个函数: typealias AuthFunction = (String, String, AuthDataResultCallback?) -> ()
我对继承有疑问。假设我有 4 节课:基类A,B类继承A,C类继承A,BC 类继承 B 和 C class A { public: void test() {
我正在尝试整理一些代码。 我有 16 个类,它们都有一些共同的功能,我用宏抽象了这些功能: #define COMMON4( CLASS, BASE, ASSIGN, CHECK ) \ ex
在接下来的代码中,在 _tmain(..) 中调用 D::f 时出现不明确的错误因为 B::f 覆盖了 A::f,所以 A::vtable 中指向 f 的指针指向 B::f。 1) 为什么编译器会给出
我是一名优秀的程序员,十分优秀!