驱动类中的重载运算符 我是studying Lafore的第四版C++书,我一直坚持这个问题
我有这两门课,CountDn是从柜台传来的。在CountDn中,我想重载减量运算符的前缀和递增和递减运算符的后缀。 它适用于所有运算符,但我尝试执行++c11时除外。 我的编译器出现以下错误: 50:10:错误:“运算符++”不匹配(操作数类型为“CountDn”) 50:10:注:候选人为: 41:13:注:CountDn CountDn::运算符++(int) 41:13:注:候选人需要1个参数, 提供0 即使get_count()工作正常,我也不明白前缀运算符为什么不工作。我的想法是,如果CounterDn类从CounterAll派生 公共功能应该是可访问的。我可以修改什么以便更好地理解此问题的解决方案驱动类中的重载运算符 我是studying Lafore的第四版C++书,我一直坚持这个问题,c++,C++,我有这两门课,CountDn是从柜台传来的。在CountDn中,我想重载减量运算符的前缀和递增和递减运算符的后缀。 它适用于所有运算符,但我尝试执行++c11时除外。 我的编译器出现以下错误: 50:10:错误:“运算符++”不匹配(操作数类型为“CountDn”) 50:10:注:候选人为: 41:13:注:CountDn CountDn::运算符++(int) 41:13:注:候选人需要1个参数, 提供0 即使get_count()工作正常,我也不明白前缀运算符为什么不工作。我的想法是,如果
#include <iostream>
using namespace std;
class Counter{
protected:
unsigned int count; //count
public:
Counter() : count(0) //constructor, no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{
return count;
}
Counter operator ++ () //incr count (prefix)
{
return Counter(++count);
}
};
class CountDn : public Counter{
public:
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c): Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{
return CountDn(--count);
}
CountDn operator --(int){
return CountDn(count --);
}
CountDn operator ++(int)
{
return CountDn(count++);
}
};
int main() {
CountDn c1(10),c2;
c2 = ++c1;
cout << c1.get_count() << endl;
return 0;
}
#包括
使用名称空间std;
班级计数器{
受保护的:
无符号整数计数;//计数
公众:
计数器():计数(0)//构造函数,无参数
{ }
计数器(int c):计数(c)//构造函数,一个参数
{ }
unsigned int get_count()常量//返回计数
{
返回计数;
}
计数器运算符++()//递增计数(前缀)
{
返回计数器(++计数);
}
};
类CountDn:公共计数器{
公众:
CountDn():Counter()//构造函数,无参数
{ }
CountDn(int c):计数器(c)//构造函数,1参数
{ }
CountDn运算符--()//递减计数(前缀)
{
返回CountDn(--count);
}
CountDn运算符--(int){
返回CountDn(count--);
}
CountDn运算符++(int)
{
返回CountDn(count++);
}
};
int main(){
计数DN c1(10),c2;
c2=++c1;
库特
添加版本前缀运算符不带参数,后缀运算符带参数。您在主函数中使用了前缀运算符,因此需要添加不带参数的函数。operator++()
和operator++(int)
是operator++
函数的两个重载
当编译器在派生类中看到operator++(int)
函数时,它不会查找该函数的其他重载。因此,在尝试编译该行时,找不到operator++()
c2 = ++c1;
因此,在基类中找不到预增量运算符。您可以使用using
声明将预增量重载从基类引入派生类
class CountDn : public Counter{
public:
using Counter::operator++;
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c): Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{
return CountDn(--count);
}
CountDn operator --(int){
return CountDn(count --);
}
CountDn operator ++(int)
{
return CountDn(count++);
}
};
现在,operator++
的两个重载都可用于CountDn
对象
然而,以下仍然是一个问题
c2 = ++c1;
由于预增量运算符返回的是计数器
对象,而不是CountDn
对象。您可以使用:
++c1;
c2 = c1;
解决这个问题。请将错误作为文本包含在问题中,使其成为虚拟并添加覆盖。我编辑了错误列表,而且我还没有学习虚拟函数,只是类,带有ctor、dctor、继承和运算符重载。在类层次结构中重新定义非虚拟函数几乎总是一个错误。Public没有虚拟析构函数的继承也是相当可疑的。operator++(int)
在派生类中隐藏operator++()
来自tge基类。如果你想在派生类中使用它,你必须在那里定义它,或者说使用CountDn::operator++;
。如果我删除int参数,编译器如何区分后缀和后缀?@Stef添加这两个版本。没有参数的是前缀运算符(在你的程序中使用)带参数的是后缀运算符。我尝试了这个解决方案,执行++c1;有效,但是当我执行c2=++c1时,这是我在函数“int main()”中得到的错误:47 5[Error]与“operator=”不匹配(操作数类型为“CountDn”和“Counter”)。47 5[Note]候选值为:22 7[Note]CountDn&CountDn::operator=(const CountDn&)22 7[注]参数1从'Counter'到'const CountDn&'没有已知的转换,我想这是因为prefix++运算符返回一个Counter对象,而c2是一个CountDnobject@Stef,完全正确。请参阅更新的答案。非常感谢,这解决了我的问题,有助于理解问题,我以前从未使用过使用命令,但我我会调查一下
++c1;
c2 = c1;