Assert()函数抛出一个错误——操作符有问题
assert() function throwing an error something wrong with the operator
我得到的错误是
whole.cpp(384): error C2270: '==' : modifiers not allowed on nonmember functions
whole.cpp(384): error C2805: binary 'operator ==' has too few parameters
whole.cpp(384): error C2274: 'function-style cast' : illegal as right side of '.' operator
我似乎无法确定问题所在,所以这里是代码
是类中的操作符实现bool operator==(const DateC& p) const{return ( DateC::DateC()== p.DateC() );};
#include <assert.h>
int main(unsigned int argc, char* argv[])
{
DateC f(29,33,11);
DateC::testAdvancesWrap();
};
void DateC::testAdvancesWrap(void)
{
DateC d;
cout << "DateC::testAdvanceWrap()" << endl ;
cout << "*********************" << endl << endl ;
cout << "tCHECK ADVANCE MULTIPLES:" << endl;
cout << "t------------------------" << endl;
d.setDay(1);
d.setMonth(12);
d.setYear(1999);
prettyPrint(d);
cout << "ACTION: set date 01-Dec-1999, advance, 31 days, 1 month and 1 year ->" << endl;
d.advance(1,1,31);
assert( d == DateC(1,2,2001) );
cout << "SUCCESS" << endl;
prettyPrint(d);
cout << endl << endl;
}
其余的功能工作良好,只有assert()
当您创建自己的类时,如果您想比较它们,则需要为它们创建操作符。假设您想比较一个类Person的两个实例。
一个人由字符串和int型(姓和高)组成。
我们希望通过身高来比较人们,所以我们需要告诉编译器如何做。一个例子:
class Person
{
string lastname;
int height;
bool operator == (const Person& p) const
{
return (this->height == p.height);
}
};
编辑:我想你误解了我的例子,你只能比较编译器知道如何比较的东西。你的Date实现可能有整型,所以如果你要检查是否相等,你必须检查所有字段。
使用this->
来访问函数中其他对象的字段
相关文章:
- 我似乎对if/else的基本语句有问题:/
- posix_spawn():使用posix_scawn()时的错误处理问题
- 输出错误,问题是找到总和5000位数字cpp
- 我在范围内未声明的错误类有问题
- 为什么这段代码给我错误? 有没有自错?
- 我的C++语言蛮力算法有问题
- C++泛型类错误,问题出在哪里?
- 我关于函数"Assert"的C++代码有问题
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++类中的友元函数有问题?
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 我对数组即 0x6dfe78 有问题
- Microsoft Visual中的此链接器错误有什么问题
- 我在 c++ 中的 AES 代码有问题。明文正在更改,但加密邮件是相同的。我找不到我的错误
- 对简单的帐户检查程序有问题?程序返回编译错误,指出"no match for 'operator||' unsure of how to fix?"
- __cdecl _unlock在声明指向指针的点抛出线程错误时与 VS C++ 2010 有问题
- Assert()函数抛出一个错误——操作符有问题
- 代码在我的系统上运行良好,但在USACO上出现分段错误.有什么问题吗?
- Playpen错误有问题
- 我的代码有问题,因为我无法弄清楚为什么会收到错误。这是代码: