Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载运算符<&书信电报;对于在模板结构中定义的成员结构_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 重载运算符<&书信电报;对于在模板结构中定义的成员结构

C++ 重载运算符<&书信电报;对于在模板结构中定义的成员结构,c++,templates,operator-overloading,C++,Templates,Operator Overloading,你好,亲爱的StackOverflowers 我对包含另一个(但非模板)结构的模板结构有问题 事情是这样的: 结构B是非模板的,在模板结构a中定义 结构B是“受保护”的,因为它仅为结构A的目的而存在,任何人、任何其他人不得在结构A之外使用它 运算符something=std::move(rhs)过载; 归还*这个; } }; B_对象; friend std::ostream&operator只需在B中内联声明运算符,它就可以工作了: ... struct B { std::string

你好,亲爱的StackOverflowers

我对包含另一个(但非模板)结构的模板结构有问题

事情是这样的:

  • 结构B是非模板的,在模板结构a中定义

  • 结构B是“受保护”的,因为它仅为结构A的目的而存在,任何人、任何其他人不得在结构A之外使用它

  • 运算符something=std::move(rhs)过载; 归还*这个; } }; B_对象;
    friend std::ostream&operator只需在
    B
    中内联声明运算符,它就可以工作了:

    ...
    struct B
    {
        std::string something;
    
        B& operator= (const std::string&& rhs)
        {
            this->something = std::move(rhs);
            return *this;
        }
    
        friend std::ostream& operator<< (std::ostream& output, const typename A<T>::B& ob)
        {
            output << ob.something;
            return output;
        }
    };
    ...
    
    。。。
    结构B
    {
    string某物;
    运算符=(常量std::string&&rhs)
    {
    这个->某物=标准::移动(rhs);
    归还*这个;
    }
    
    朋友std::奥斯特拉姆与运营部你真的帮了大忙-谢谢!但在某种程度上我的问题仍然成立。我要做什么才能得到它operator@BR41N-FCK:问题是您的
    操作员
    
    #include <iostream>
    #include <string>
    
    template <typename T>
    struct A
    {
        T variable;
    
    protected:
        struct B
        {
            std::string something;
    
            B& operator= (const std::string&& rhs)
            {
                this->something = std::move(rhs);
                return *this;
            }
        };
    
        B B_object;
    
        template <typename X> friend std::ostream& operator<< (std::ostream& output, typename A/*<X>*/::B& ob);
    
    public:
        void method ()
        {
            B_object = "This is text.";
    
            //ERROR: Invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'A<int>::B')
            std::cout << B_object;
        }
    };
    
    
    
    template <typename X>
    std::ostream& operator<< (std::ostream& output, typename A<X>::B& ob)
    {
        output << ob.something;
        return output;
    }
    
    
    
    int main(int argc, const char * argv[])
    {
        A<int> obj;
        obj.method();
    
        return 0;
    }
    
    ...
    struct B
    {
        std::string something;
    
        B& operator= (const std::string&& rhs)
        {
            this->something = std::move(rhs);
            return *this;
        }
    
        friend std::ostream& operator<< (std::ostream& output, const typename A<T>::B& ob)
        {
            output << ob.something;
            return output;
        }
    };
    ...