[英]“Error C3867: non-standard syntax; use '&' to create a pointer to member” when using virtual stream manipulator
In my code in Visual Studio 2015 i have interface: 在Visual Studio 2015中的代码中,我具有以下界面:
struct IStr
{
virtual std::ostream& beginMessage() = 0;
virtual std::ostream& endMessage(std::ostream&) = 0;
};
And i have a class that implements this interface, like this: 我有一个实现此接口的类,如下所示:
#include <sstream>
struct MyStr : public IStr
{
std::stringstream m_stream;
std::ostream& beginMessage() override { return m_stream; }
std::ostream& endMessage(std::ostream& ss) override { return std::endl(ss); }
};
However, i am getting an error when trying to compile simple code: 但是,尝试编译简单代码时出现错误:
IStr * pStr = new MyStr();
pStr->beginMessage() << "Hello Wordl!" << pStr->endMessage;
With message: 带有消息:
Error C3867
'IStr::endMessage': non-standard syntax; use '&' to create a pointer to member
I really like syntax i am trying to use. 我真的很喜欢我想使用的语法。 But is this possible? 但这有可能吗? Maybe the problem is that my manipulator is virtual, or non-static? 也许问题是我的操纵器是虚拟的还是非静态的?
Use NVI, and have endMessage()
return a (stateful) manipulator that calls the virtual function on the stream when streamed. 使用NVI,并让endMessage()
返回一个(有状态的)操纵器,该操纵器在流式处理时在流上调用虚拟函数。
struct IStr
{
// other stuff
private:
virtual std::ostream& doBeginMessage() = 0;
virtual std::ostream& doEndMessage(std::ostream&) = 0;
struct EndManip{
IStr* istr;
};
friend std::ostream& operator<<(std::ostream& ss, EndManip em){
return em.istr->doEndMessage(ss);
}
public:
EndManip endMessage() { return {this}; }
std::ostream& beginMessage() { return doBeginMessage(); }
};
With this you'd do pStr->beginMessage() << "Hello World!" << pStr->endMessage();
这样,您就可以执行pStr->beginMessage() << "Hello World!" << pStr->endMessage();
pStr->beginMessage() << "Hello World!" << pStr->endMessage();
. 。
You have written: 你写:
IStr * pStr = new MyStr();
pStr->beginMessage() << "Hello Wordl!" << pStr->endMessage;
while you should have written something similar to: 而您应该编写类似于以下内容的内容:
IStr * pStr = new MyStr();
pStr->beginMessage() << "Hello Wordl!" << pStr->endMessage(*pStream);
The difference here is in the (), which results in doing the function call instead of trying to pass a pointer to the function (which is most likely not your intent). 这里的区别在于(),这导致执行函数调用,而不是尝试将指针传递给函数(这很可能不是您的意图)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.