- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在尝试开发一个继承自 std::stringbuf
的类时和 std::ostream
并给它一个自定义 operator<<()
重载(应该适用于此类,但一般不适用于 std::ostream
),我遇到了一些我不理解的歧义错误。
问题出在下面代码的下半部分。如果我删除 operator<<()
过载,我没有收到任何错误(g++ --std=c++17 -o foo.exe foo.cpp
使用 g++ 9.2.0,MSYS2 在 Windows 上构建)。
#include <iostream>
#include <ostream>
#include <sstream>
class MyStream:
private std::stringbuf,
public std::ostream
{
public:
MyStream() noexcept:
std::stringbuf(),
std::ostream(this)
{}
~MyStream()
{
if (pptr() - pbase() < 2)
{ sync(); }
}
protected:
virtual int sync() override
{
// make sure there actually is something to sync
if (pptr() - pbase() < 2)
{ return 0; }
const std::string message{str()};
const std::size_t length{message.length()};
// update the pointers for the next sync
setp(pbase() + length, epptr());
std::cout << message;
return std::stringbuf::sync();
}
};
MyStream& operator<<(MyStream& out, bool boolean) noexcept
{ return out << (boolean ? "yes" : "no"); }
/* ↑
more than one operator "<<" matches these operands:
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<char, _Traits>
&std::operator<<(std::basic_ostream<char, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const _Elem *_Val)"
-- function "operator<<(MyStream &out, bool boolean) noexcept"
-- operand types are: MyStream << const char *
*/
int main()
{
MyStream stream;
stream << "Hello World" << std::endl;
/* ↑
more than one operator "<<" matches these operands:
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<char, _Traits>
&std::operator<<(std::basic_ostream<char, _Traits> &_Ostr, const char *_Val)"
-- function template "std::basic_ostream<_Elem, _Traits>
&std::operator<<(std::basic_ostream<_Elem, _Traits> &_Ostr, const _Elem *_Val)"
-- function "operator<<(MyStream &out, bool boolean) noexcept"
-- operand types are: MyStream << const char [12]
*/
}
为什么我的 bool
过载了? char*
的有效候选人争论?我怎样才能正确地做到这一点?
最佳答案
This似乎在 linux (g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0) 下对我有用:
MyStream& operator<<(MyStream& out, bool boolean)
{
out << (boolean ? "yes" : "no");
return out;
}
我只更改了返回调用。那是因为 out << (boolean ? "yes" : "no")
将使用基础 ostream operator<<
对于 char*
类型,因此返回 std::basic_ostream<char>
输入而不是 MyStream
返回值类型。
然后在main()
使用 bool
输入 MyStream operator<<
作品:
int main()
{
MyStream stream;
bool b=true;
stream << b << "Hello World" << b << std::endl;
//by contrast, this should output "yesHello Worldyes"
//stream << b << "Hello World";
//stream << b << std::endl;
//and so would this kind of ugliness! lol
//static_cast<MyStream&>(stream << b << "Hello World") << b << std::endl;
}
..但是一旦基类ostream operator<<
为 "Hello World"
调用声明的一部分,它当然会返回一个 std::basic_ostream<char>
输入 :)
..然后在那之后,因为不存在 ostream operator<<
对于 bool
类型,bool b
类型将隐式提升为 int
所以输出是:
yesHello World1
!
但是您似乎遇到了其他错误。因此,虽然我手边没有 MSYS2,但我确实尝试使用 Visual Studio 在 WinOS 上编译代码。就我而言,至少,你的 main()
由于 VS STL 实现引起的歧义,立即失败:
int main()
{
MyStream stream;
stream << "Hello World" << std::endl;
}
无需怀疑您当前的方法,因为评论中已经出现了这种情况。但只是为了回答您的问题;那么一种方法是实现另一个 operator<<
对于 MyStream
处理 char*
类型:
#include <iostream>
#include <ostream>
#include <sstream>
class MyStream:
private std::stringbuf,
public std::ostream
{
public:
MyStream() noexcept:
std::stringbuf(),
std::ostream(this)
{}
~MyStream()
{
if (pptr() - pbase() < 2)
{ sync(); }
}
protected:
virtual int sync() override
{
// make sure there actually is something to sync
if (pptr() - pbase() < 2)
{ return 0; }
const std::string message{str()};
const std::size_t length{message.length()};
// update the pointers for the next sync
setp(pbase() + length, epptr());
std::cout << message;
return std::stringbuf::sync();
}
};
MyStream& operator<<(MyStream& out, const char* str)
{
static_cast<std::ostream&>(out) << str;
return out;
}
MyStream& operator<<(MyStream& out, bool boolean) noexcept
{ return out << (boolean ? "yes" : "no"); }
int main()
{
MyStream stream;
bool b=1;
stream << b << " oook " << b << std::endl;
}
或者,由于 MyStream
实际上是源自ostream
然后我们可以使用任何 operator<<
对于 ostream
带有显式强制转换的类型。例如:
#include <iostream>
#include <ostream>
#include <sstream>
class MyStream:
private std::stringbuf,
public std::ostream
{
public:
MyStream() noexcept:
std::stringbuf(),
std::ostream(this)
{}
~MyStream()
{
if (pptr() - pbase() < 2)
{ sync(); }
}
protected:
virtual int sync() override
{
// make sure there actually is something to sync
if (pptr() - pbase() < 2)
{ return 0; }
const std::string message{str()};
const std::size_t length{message.length()};
// update the pointers for the next sync
setp(pbase() + length, epptr());
std::cout << message;
return std::stringbuf::sync();
}
};
std::ostream& operator<<(MyStream& out, bool boolean) noexcept
{
return static_cast<std::ostream&>(out) << (boolean ? "yes" : "no");
}
int main()
{
MyStream stream;
bool b=1;
stream << b << " ooOok ";
stream << b << std::endl;
}
希望输出的位置:
yes ooOok yes
关于c++ - 自定义 std::ostream 子类中 operator<< 重载的歧义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58486557/
我正在开发一个小型图书馆,我需要做的一件事是让访问者访问一些数据并返回结果。 在一些较旧的 C++ 代码中,访问者需要声明一个 typedef return_type .例如,boost::stati
我不明白这个 ostream 函数声明是什么意思: ostream& operator<< (ostream& (*pf)(ostream&)); (特别是 (*pf)(ostream&) 部分)。我
我正在寻找一种将内容从一个 ostream 复制到另一个的方法。我有以下代码: std::ostringsteam oss; oss << "stack overflow"; { //do s
我有一个装满小雕像的游戏板。 分配: board = new Figure*[boardSize]; for (int i = 0; i > > board(boardSize, vector>(b
代码: cout SomeStream &operator SomeStream &operator (*this) << val; //Trouble in there! std::co
我正在对我的项目进行一些类型检查。下面的例子 using namespace std; cout ::value ? "TRUE":"FALSE" ) (); } 格式正确(source)。 std:
我到处搜索,但没有找到答案,所以如果这是重复的,请原谅。 我有一些非常古老的 C++ 代码,我正试图将它们轻松地转换为这个千年。代码仍然在 Visual Studio 6 中编译并且需要继续这样做,但
在我的 C++ 代码中,我不断地将不同的值写入文件。我的问题是,考虑到文件已成功打开这一事实,如果在任何情况下 write 或 << 会失败。我是否需要检查每次调用 write 或 << 以确保它已正
这个问题在这里已经有了答案: Calling a function overloaded in several namespaces from inside one namespace (3 个答案
我只是在写一些代码来吐出一个 wave header。我开始输入: file > 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com
我有一个派生自 std::ostream 的类(用于日志记录)。剥离下来,它看起来像这样: class bsgs : public std::ostream { public: bsgs(cons
我有以下代码: class A { public: ... C *func() { ... } void func2() { ... } ... }; class B
我在一个抽象类的头文件中找到了这个函数: virtual ostream & print( ostream & out ) const; 谁能告诉我这是什么函数以及如何在派生类中声明它?据我所知,它似
我想写一个函数输出一些东西到 ostream传入并返回流,如下所示: std::ostream& MyPrint(int val, std::ostream* out) { *out << val
偶尔我会写一个类(T 说)并尝试覆盖 std::ostream& operator<<(std::ostream&, const T&) 但它不适用于某些类。这是一个(简化的)类的示例,它对我不起作用
我一直在尝试创建一个程序来为一组已定义的进程实现实时调度算法。使用 g++ 编译时出现错误,其中指出: RTSprocess.h:在函数“std::ostream& operator #include
因此,我得到了一个带有起始代码的任务来实现一个链表(我已经成功地完成了一个未排序的双向链表)并且在给定头文件的起始代码中有一个友元声明似乎有允许我使用 cout 打印链表的目标陈述。这是头文件;请注意
我正在努力 cout using namespace std; ostream& Print(ostream& out) { out using namespace std; ostream&
为了序列化任何对象(即对于没有全局 ostream& operator std::ostream& operator std::string serialize_any(const T& val) {
我宁愿实现一个non-friend函数,直接将函数标记为virtual。 但我现在想确保一组特定的类实现重载 friend std::ostream& operator << (std::ostrea
我是一名优秀的程序员,十分优秀!