简体   繁体   English

“错误C3867:非标准语法; 使用虚拟流操纵器时,使用“&”创建指向成员的指针”

[英]“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.

相关问题 C3867:&#39;_com_error :: Description&#39;:非标准语法 使用“&”创建指向成员的指针 - C3867 : '_com_error::Description': non-standard syntax; use '&' to create a pointer to member Visual Studio 2015 错误 C3867 &#39;Rectangle::get_area&#39;:非标准语法; 使用“&amp;”创建指向成员的指针 - Visual Studio 2015 Error C3867 'Rectangle::get_area': non-standard syntax; use '&' to create a pointer to member c3867&#39;sf::Time::as seconds&#39;:非标准语法;使用&#39;&amp;&#39;创建指向成员的指针 - c3867'sf::Time::as seconds':non-standard syntax ;use '&'to create a pointer to a member std::unordered_map 中的 std::function,错误 C3867 非标准语法; 使用 '&amp;' 创建指向成员的指针 - std::function in std::unordered_map, error C3867 non-standard syntax; use '&' to create a pointer to member 获取错误:C3867 &#39;Template_class<int> ::add&#39;: 非标准语法; 使用带有模板的函数指针时 - Getting error : C3867 'Template_class<int>::add': non-standard syntax; when using pointers to functions with templates c ++非标准语法; 使用“&”创建具有虚拟功能的成员的指针 - c++ non-standard syntax; use '&' to create a pointer to member with virtual function 非标准语法; 使用 '&amp;' 创建指向成员 C++ 的指针 - non-standard syntax; use '&' to create a pointer to member C++ 错误信息:非标准语法; 使用“&amp;”创建指向成员的指针 - Error Message: non-standard syntax; use '&' to create a pointer to member 尝试使用指向函数的指针将模板化 class 的方法传递给另一个 class 时,出现错误 C3867 使用“&”创建指向成员的指针 - While trying to pass a method of a templated class to another class using pointer to functions,get Error C3867 use '&' to create a pointer to a member 非标准语法; 将成员函数分配给vector时,使用&#39;&&#39;创建指向成员错误的指针 - non-standard syntax; use '&' to create a pointer to member error when assigning member function to vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM