4

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:\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

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();
}
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
KK.
  • 77
  • 2
  • 5
  • The GetHashCode implementation should use the same variables you use in your Equals implementation, such as : int Sample::GetHashCode() { return someVariable->GetHashCode(); } – Rodney Richardson Mar 22 '13 at 11:16

2 Answers2

7

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
    //...
}
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • I changed Equal to Equals and removed override from the source file but error 2 still appears. – KK. Dec 02 '09 at 09:38
  • Your `GetHashCode` implementation is `FgStatusWrapper::GetHashCode` while the declaration is `Sample::GetHashCode`. – Mehrdad Afshari Dec 02 '09 at 09:41
  • The error is as follows: Error 1 error LNK2022: metadata operation failed (80131187) : Inconsistent method declarations in duplicated types (types: Test.Sample; methods: Equals): (0x06000002). Sample.obj – KK. Dec 02 '09 at 09:48
  • Try Clean project and then rebuild. Also, make sure your header files have `#pragma once` on top. – Mehrdad Afshari Dec 02 '09 at 09:55
0

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.

Lopper
  • 3,499
  • 7
  • 38
  • 57