【发布时间】:2016-05-12 15:21:23
【问题描述】:
我正在尝试开发一个允许收集相关数据流样式的异常类。
按照Custom stream to method in C++?,我扩展了自己的课程:
class NetworkException : public std::exception, public std::ostream
从流中挑选错误数据,然后返回它通过.what() 获取的任何内容。
然后我尝试了这样的事情:
try {
ssize_t ret = send(sock, headBuffer, headLength, MSG_MORE);
if (ret <= 0) throw NetworkException() << "Error sending the header: " << strerror(errno);
// much more communication code
} catch (NetworkException& e) {
connectionOK = false;
logger.Warn("Communication failed: %s",e.what());
}
但是编译器会报错:
HTTPClient.cpp:246:113: error: use of deleted function 'std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)'
(这是throw的行。)
我意识到流没有复制构造函数,但我认为捕获引用而不是对象就足够了。我该如何克服这个问题 - 我可以抛出一个“吞下”流的对象吗?
【问题讨论】:
-
@MohamadElghawi:那么这就变成了“我能以某种方式为流对象创建一个复制构造函数吗?”
标签: c++ exception iostream ostream