【问题标题】:C++ Overloaded Output Operator with Template带有模板的 C++ 重载输出运算符
【发布时间】:2018-10-06 19:01:17
【问题描述】:

我正在尝试重载输出运算符以从具有使用非类型值的模板的类中打印。但是,我不断收到错误

“意外的令牌'标识符',预期的';'”

在操作符的函数体中。如何修复我的朋友声明或运算符重载定义以避免此错误?

template <int N, int M> class Screen {
    friend std::ostream& operator<< (std::ostream&, const Screen&);
public:
    Screen(): width(N), height(M) {}
    int width = 0;
    int height = 0;
};

template <int N, int M>
std::ostream& operator<< (std::ostream& os, const Screen<N, M>& a)
{
    os << a.width << ":" a.height;
    return os;
}

【问题讨论】:

    标签: c++ templates operator-overloading non-type


    【解决方案1】:

    你忘记了&lt;&lt;

    // ..................VV
    os << a.width << ":" << a.height;
    

    【讨论】:

    • @MildMax - 发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多