KK.*_*KK. 4 compiler-errors c++-cli compiler-warnings method-declaration
我是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
Run Code Online (Sandbox Code Playgroud)
编辑3:我将"Equal"更改为"Equals",删除了源文件中的override关键字,但错误2仍然存在.
//头文件
public ref class Sample : public Object
{
public:
int someVariable;
virtual bool Equals(Object^ obj) override;
virtual int GetHashCode() override;
}
Run Code Online (Sandbox Code Playgroud)
// 源文件
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();
}
Run Code Online (Sandbox Code Playgroud)
方法的名称不是Equal,它是Equals.您不应该在实现中使用virtual或override关键字:
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
//...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8109 次 |
| 最近记录: |