没有匹配的“运营商<<”

No match for 'Operator<<'

本文关键字:lt 运营商      更新时间:2023-10-16

在此程序中,我得到了错误

[错误]"操作员&lt;&lt;''无匹配(操作数类型是'std :: ostream {aka std :: basic_ostream}'和'numcall')

我不明白如何摆脱它!

#include<iostream>
using namespace::std;

class numcall
{
int a,b;
public:
     numcall(int c,int d)
    {
        c = a;
        c = b;
        cout<<"Your First num is " << c << endl << "Your Second num is "<< d << endl;
        cout << "You are in PARAMETER CONSTRUCTOR";
    }   
    numcall(int u)
    {
        u = a;
        b = 0;
        cout << "Your First num is " << u << endl << "Your Second num is " << b << endl; 
        cout << "You are in PARAMETER CONSTRUCTOR";
    }
    numcall()
    {
    }
};
int main(void)
{
    numcall x = numcall();
    numcall y = numcall(3,4);
    numcall z = numcall(3);
    cout << x << endl << endl << y << endl << endl << z << endl;
}

您尚未为类numcall定义<<操作员,因此编译器不知道如何应用。

因此定义它。

您需要为&lt;&lt;流操作员,否则他不知道要打印什么。

  friend ostream &operator<<(ostream &os, const numcall &numcall1) {
    os << "a: " << numcall1.a << " b: " << numcall1.b;
    return os;
}

这只是实现的一个示例。

顺便说一句,还有其他错误:c =一种分配给c的手段,您想以相反的方式做。欢迎来到编程世界伴侣;)