Matt Arnold
Matt Arnold

Reputation: 686

How do you Qualify a Type Name in a Constructor for an Object which has a Member of the Same Name?

Suppose I have the following 2 structs.

struct ErrorCodes
{
    const unsigned long UsbErrorCode
    const DWORD DriverErrorCode;

    ErrorCodes(const unsigned long usbErrorCode, const DWORD driverErrorCode)
        : UsbErrorCode(usbErrorCode),
        WinDriverErrorCode(winDriverErrorCode)
    {
    }
}

struct Response
{
    const char* Message;
    const ErrorCodes ErrorCodes;

    Response(const char* message, const ErrorCodes errorCodes)
        : Message(message),
        ErrorCodes(errorCodes)
    {
    }
}

How do I qualify const ErrorCodes in the constructor of Response so that the compiler knows I'm referring to the type ErrorCodes and not the member ErrorCodes? I don't want to change names as this needs to map-up to a C# struct for interop.

The compiler error I currently get is: member "Response::ErrorCodes" is not a type name.

Upvotes: 0

Views: 46

Answers (2)

4xy
4xy

Reputation: 3662

Just add :: to ErrorCodes type if it's in global namespace or ns:: if it's in namespace ns or whatever name you would like. Like ::ErrorCodes. And as mentioned by @goodvibration in comment it's better avoid such name collisions.

const ::ErrorCodes ErrorCodes;

:: is the scope resolution operator.

Upvotes: 1

super
super

Reputation: 12928

You can rename the type. Or move it into a separate namespace.

namespace ns {
struct ErrorCodes
{
    const unsigned long UsbErrorCode;
    const int DriverErrorCode;

    ErrorCodes(const unsigned long usbErrorCode, const int driverErrorCode)
        : UsbErrorCode(usbErrorCode),
        DriverErrorCode(driverErrorCode)
    {
    }
};
}

struct Response
{
    const char* Message;
    const ns::ErrorCodes ErrorCodes;

    Response(const char* message, const ns::ErrorCodes errorCodes)
        : Message(message),
        ErrorCodes(errorCodes)
    {
    }
};

Upvotes: 0

Related Questions