Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++;具有模板依赖类型_C++_Templates_Operator Overloading_Friend - Fatal编程技术网

C++ 重载运算符“<<&引用;在C++;具有模板依赖类型

C++ 重载运算符“<<&引用;在C++;具有模板依赖类型,c++,templates,operator-overloading,friend,C++,Templates,Operator Overloading,Friend,我正在尝试重载“#include” 模板 类主类 { 公众: 类相关的 { 公众: 依赖类():val(0){} friend std::ostream&operator移动运算符的定义您正在声明一个作为模板模板的friend函数,但您声明的全局函数只是一个以模板类型为参数的函数。您可能只需要从friend前面删除模板解释错误是什么,以及你的代码是如何修复它们的,这会使答案更有用。此外,你应该忽略原始代码中大部分没有更改的部分——只需留出足够的内容(如果必要的话)。从友元声明之前删除模板可解决我

我正在尝试重载“
#include”
模板
类主类
{
公众:
类相关的
{
公众:
依赖类():val(0){}

friend std::ostream&operator移动
运算符的定义您正在声明一个作为模板模板的friend函数,但您声明的全局函数只是一个以模板类型为参数的函数。您可能只需要从
friend
前面删除
模板解释错误是什么,以及你的代码是如何修复它们的,这会使答案更有用。此外,你应该忽略原始代码中大部分没有更改的部分——只需留出足够的内容(如果必要的话)。从友元声明之前删除
模板
可解决我的原始错误,但会导致另一个错误。删除
模板
时,我在Visual Studio中遇到链接器错误(LNK2019:“函数_main中引用的未解析外部符号”)@guyoverthere522您还需要按照答案中的说明将函数定义移到类中。虽然生成的代码是正确的,但提供的原因解释却不正确。当然,模板参数T的阴影已经存在,但核心问题是自由模板函数。如果您想了解更多信息,请参阅非常棒的答案
#include <iostream>

template<class T>
class main_class
{
public:
    class dependent_class
    {
    public:
        dependent_class() : val(0) { }
        template<class T>
        friend std::ostream& operator<<(std::ostream&, const dependent_class&);
    protected:
        int val;
    };
};

template<class T>
std::ostream& operator<<(std::ostream& out, const typename main_class<T>::dependent_class& v)
{
    return out << v.val;
}

int main(int argc, char* argv[])
{
    main_class<int>::dependent_class v;
    std::cout << v; // error: no operator "<<" matches these operands
    //      operand types are: std::ostream << main_class<int>::dependent_class

    return 0;
}
#include <iostream>

template<class T>
class main_class
{
public:
    class dependent_class
    {
    public:
        dependent_class() : val(0) { }

        friend std::ostream& operator<<(std::ostream& out, const main_class<T>::dependent_class& v){
            return out << v.val;
        }
    protected:
        int val;
    };
};



int main(int argc, char* argv[])
{
    main_class<int>::dependent_class v;
    std::cout << v; // error: no operator "<<" matches these operands
    //      operand types are: std::ostream << main_class<int>::dependent_class

    return 0;
}
 class dependent_class
    {
    public:
        dependent_class() : val(0) { }
        template<class T> //here
        friend std::ostream& operator<<(std::ostream&, const dependent_class&);
    protected:
        int val;
    };
#include <iostream>

template<class T>
class main_class
{
public:
    class dependent_class
    {
    public:
        dependent_class() : val(0) { }
        friend std::ostream& operator<<(std::ostream& out, const dependent_class& v)
        {
            return out << v.val;
        }

    protected:
        int val;
    };
};




int main(int argc, char* argv[])
{
    main_class<int>::dependent_class v;
    std::cout << v; // error: no operator "<<" matches these operands
    //      operand types are: std::ostream << main_class<int>::dependent_class

    return 0;
}