与'运营商<<'不匹配

时间:2017-12-05 16:47:26

标签: c++

在这个程序中我收到了错误

[错误]与'operator<<'不匹配(操作数类型是'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;
}

2 个答案:

答案 0 :(得分:1)

您还没有为您的班级<<定义numcall运算符,因此编译器并不知道如何应用它。

所以定义它。

答案 1 :(得分:0)

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

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

这只是一个实施的例子。

顺便说一下,还有其他的错误:c = a指的是指向c,你想要反过来做。欢迎来到编程世界的伴侣;)