【问题标题】:Overloading operator "<<" in C++ with template-dependent type在 C++ 中使用模板相关类型重载运算符“<<”
【发布时间】:2020-05-24 03:40:20
【问题描述】:

我正在尝试重载“

#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;
}

【问题讨论】:

  • 您声明了一个友元函数,它是模板的模板,但您声明的全局函数只是一个将模板类型作为参数的函数。您可能只需要从friend 前面删除template&lt;class T&gt;

标签: c++ templates operator-overloading friend


【解决方案1】:
#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;
}

您的代码中有错误。

在dependent_class 中,您使用模板两次,这会影响模板参数。

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

【讨论】:

  • 添加对错误是什么以及您的代码如何修复它们的解释将使答案更有帮助。此外,您应该省略大部分未更改的原始代码 - 只为上下文留出足够的内容(如果需要)。
  • 从朋友声明之前删除 template&lt;class T&gt; 可以解决我原来的错误,但会导致另一个错误。删除 template&lt;class T&gt; 后,我在 Visual Studio 中收到链接器错误(LNK2019:“函数 _main 中引用的未解析的外部符号”)
  • @guyoverthere522 你还需要在类中移动函数定义,如答案中所述
  • 虽然生成的代码是正确的,但提供的原因解释却不正确。当然,模板参数 T 的影响是存在的,但核心问题是免费模板功能中的"non-deducible context"。如果您想了解更多信息,请查看this question 的精彩答案
【解决方案2】:

operator&lt;&lt;的定义移到dependent_class的范围内,并在声明前删除template&lt;class T&gt;

#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;
}

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2012-11-14
    • 2019-01-06
    • 2012-04-26
    • 1970-01-01
    相关资源
    最近更新 更多