Skip to content

Revamp C2385 #4857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 34 additions & 37 deletions docs/error-messages/compiler-errors-1/compiler-error-c2385.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,63 @@
---
description: "Learn more about: Compiler Error C2385"
title: "Compiler Error C2385"
ms.date: "11/04/2016"
ms.date: "1/19/2024"
f1_keywords: ["C2385"]
helpviewer_keywords: ["C2385"]
ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417
---
# Compiler Error C2385

ambiguous access of 'member'
> ambiguous access of 'member'

The member can derive from more than one object (it is inherited from more than one object). To resolve this error,
A member is inherited from more than one base type, making unqualified access to that member ambiguous. To resolve this error:

- Make the member unambiguous by providing a cast.

- Rename the ambiguous members in the base classes.
- Explicitly qualify access to the member.
- Cast the object to the base class containing the member before accessing the member.
- Rename the ambiguous member in the base class.
- Bring the member into scope.

## Example

The following sample generates C2385.
The following sample generates C2385:

```cpp
// C2385.cpp
// C2385 expected
#include <stdio.h>

struct A
{
void x(int i)
{
printf_s("\nIn A::x");
}
void func1(int i) {}
void func2() {}
};

struct B
{
void x(char c)
{
printf_s("\nIn B::x");
}
void func1(char c) {}
void func2() {}
};

// Delete the following line to resolve.
struct C : A, B {}

// Uncomment the following 4 lines to resolve.
// struct C : A, B
// {
// using B::x;
// using A::x;
// };
struct C : A, B
{
// Uncomment the following lines to resolve the first 2 errors
// The error below for the call to c.func2() will remain
// using A::func1;
// using B::func1;
};

int main()
{
C aC;
aC.x(100);
aC.x('c');
}
C c;

struct C : A, B
{
using B::x;
using A::x;
};
c.func1(123); // C2385
c.func1('a'); // C2385
c.func2(); // C2385

c.A::func2(); // OK because explicitly qualified
c.B::func2(); // OK because explicitly qualified
static_cast<A>(c).func2(); // OK because of the cast
static_cast<B>(c).func2(); // OK because of the cast
}
```

You can resolve the ambiguous calls to `func1` by bringing both overloads into scope. However, this doesn't work for `func2` because `A::func2` and `B::func2` don't take arguments, so calling them can't be differentiated by their parameters. You can resolve the issue by:
- Introduce the one you want to use into scope
- Explicitly qualify the call with the base type
- Cast the object before calling the function.