gpt4 book ai didi

c++ - 运算符 << 类的重载

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:09 24 4
gpt4 key购买 nike

<分区>

A 是我写的一个C++类:

class A
{
private:
int _num1;
int _num2;
public:
A(int num1, int num2)
{
_num1 = num1;
_num2 = num2;
}

~A(){}

int getNum1() { return _num1; };
std::ostream &operator<<(std::ostream &os, A const &obj) { return os << obj.getNum1(); };
};

有一个模板函数printArray:

template <class T>
void printArray(T* arr, int size)
{
int i = 0;
for (i = 0; i < size; i++)
cout << arr[i] << endl;
}

当我在main函数中写的时候:

    A arr4[4] = { A(1,1), A(1,4), A(6,6), A(0,0) };
printArray(arr4, 4);

有这样的错误:

E0344 too many parameters for this operator function.

C2804 binary 'operator <<' has too many parameters.

C2333 'A::operator <<': error in function declaration; skipping function body.

问题是什么以及如何解决?

of course I included iostream

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com