gpt4 book ai didi

c++-cli - C++/CLI : How to override Equal method of Object class

转载 作者:行者123 更新时间:2023-12-05 00:42:40 27 4
gpt4 key购买 nike

我是 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:我将“等于”更改为“等于”,删除了源文件中的覆盖关键字,但错误 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();
}

最佳答案

方法名不是Equal ,它是 Equals .你不应该使用 virtualoverride实现中的关键字:

ref class Test {
public:
virtual bool Equals(Object^ o) override;
virtual int GetHashCode() override;

};
bool Test::Equals(Object^ o) { // no "override" here
//...
}
int Test::GetHashCode() { // no "override" here
//...
}

关于c++-cli - C++/CLI : How to override Equal method of Object class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1831696/

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