我是 C++/CLI 的新手,在尝试覆盖基 Object 类的 Equal 方法时遇到了一些问题。我收到以下代码的以下编译警告错误。这应该如何纠正?
Warning 1 warning C4490: 'override' : incorrect use of override specifier; 'Test::Sample::Equal' does not match a base ref class method c:\project\code\Sample.h 18
Error 2 error LNK2022: metadata operation failed (80131187) : Inconsistent method declarations in duplicated types (types: Test.Sample; methods: Equal): (0x06000002). Sample.obj
编辑 3:我将“Equal”更改为“Equals”,删除了源文件中的 override 关键字,但错误 2 仍然存在。
// 头文件
public ref class Sample : public Object
{
public:
int someVariable;
virtual bool Equals(Object^ obj) override;
virtual int GetHashCode() override;
}
// 源文件
bool Sample::Equals(Object^ obj)
{
if ( obj == nullptr || GetType() != obj->GetType() )
return false;
Sample^ p = dynamic_cast<Sample^>(obj);
return (someVariable == p->someVariable);
}
int Sample::GetHashCode()
{
return GetHashCode();
}