gpt4 book ai didi

c++ - 运算符重载错误不匹配运算符<<

转载 作者:行者123 更新时间:2023-11-28 01:16:45 24 4
gpt4 key购买 nike

我正在学习 C++ 中的运算符重载我创建了一个程序来重载后缀和前缀的增量运算符

它显示错误 no match for operator<<

#include <iostream>

using namespace std;
class counter
{
unsigned int countx;
public:
counter(): countx(0)
{}
counter (int c) : countx(c)
{}
unsigned int get_count() const
{
return countx;
}
counter operator ++ ()
{
return counter(++countx);
}
counter operator ++ (int)
{
return counter(countx++);
}
};

int main()
{
counter c1, c2;
cout<<endl<<"c1 : "<<c1.get_count;
cout<<endl<<"c2 : "<<c2.get_count;

++c1;
c2 = ++c1;
cout<<endl<<"c1 : "<<c1.get_count;
cout<<endl<<"c2 : "<<c1.get_count;

c2= c1++;
cout<<endl<<"c1 : "<<c1.get_count;
cout<<endl<<"c2 : "<<c2.get_count;

return 0;
}

能否请您提出一些建议来解决这个问题,并解释为什么会出现此错误?

最佳答案

我的编译器 (llvm) 说:

error: reference to non-static member function must be called; did you mean to call it with no arguments?
cout<<endl<<"c1 : "<<c1.get_count;
~~~^~~~~~~~~
()

建议您调用电话:

cout<<endl<<"c1 : "<<c1.get_count();

关于c++ - 运算符重载错误不匹配运算符<<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58448618/

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