English 中文(简体)
C++/CLI : How to override Equal method of Object class
原标题:

I am a newbie to C++/CLI and is having some problems trying to override the Equal method of the base Object class. I get the following compilation warning error for the following code. How should this be corrected?

Warning 1   warning C4490:  override  : incorrect use of override specifier;  Test::Sample::Equal  does not match a base ref class method   c:projectcodeSample.h    18  

Error   2   error LNK2022: metadata operation failed (80131187) : Inconsistent method declarations in duplicated types (types: Test.Sample; methods: Equal): (0x06000002).  Sample.obj

Edit 3: I changed "Equal" to "Equals", removed override keyword in source file but error 2 still stands.

// Header File

public ref class Sample : public Object
{
    public:
        int someVariable;

    virtual bool Equals(Object^ obj) override;
    virtual int GetHashCode() override;
}

// Source File

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();
}
最佳回答

The name of the method is not Equal, it s Equals. You shouldn t use virtual or override keywords in the implementation:

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
    //...
}
问题回答

The following is extracted from here:

According to the MSDN,one reason for LNK2022 is when a struct exists in multiple modules with the same name, but with conflicting definitions, and when you compile with /clr. This usually happens because somehow the compiler puts slightly different metadata in two modules for the same type. At link time, when the metadata is merged, this error is emitted because the name for the type is the same, but there is some descrepancy in the rest of the metadata describing that type.





相关问题
C++/CLI : How to override Equal method of Object class

I am a newbie to C++/CLI and is having some problems trying to override the Equal method of the base Object class. I get the following compilation warning error for the following code. How should this ...

处理多重定义错误[封闭]

I m试图汇编一些C++文档。 • 在连接阶段(汇编工作)发现以下几类错误:

What s causing this error in a subclass of MKAnnotationView?

I m trying to create a subclass of MKAnnotationView for an iPhone Mapkit app, but for some reason I am suddenly encountering this error: Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/...

iphlpapi / ifdef.h

I m trying to use iphlpapi (GetAdapterInfo) and am having trouble compiling the code. I have iphlpapi.h from SDK 7 and have added the appropriate path to the include files in visual studio. I get ...

was not declared in this scope C++

Why do I get this error in the code below? class ST : public Instruction{ public: ST (string _name, int _value):Instruction(_name,_value){} void execute(int[]& anArr, int aVal){ //...

Adding an include guard breaks the build

I added #ifndef..#define..#endif to a file of my project and the compiler fails. As soon as I remove it or put any other name in the define it compiles fine. What could be the problem? Sounds like ...

<list> retreving items problem with iterator

I have a list of type Instruction*. Instruction is a class that I made. This class has a function called execute(). I create a list of Instruction* list<Instruction*> instList; I create an ...

热门标签