Skip to content

Commit 8e05170

Browse files
authored
Merge pull request #4857 from Rageking8/revamp-c2385
Revamp C2385
2 parents 77e1924 + 8bb356c commit 8e05170

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed
Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,63 @@
11
---
22
description: "Learn more about: Compiler Error C2385"
33
title: "Compiler Error C2385"
4-
ms.date: "11/04/2016"
4+
ms.date: "1/19/2024"
55
f1_keywords: ["C2385"]
66
helpviewer_keywords: ["C2385"]
7-
ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417
87
---
98
# Compiler Error C2385
109

11-
ambiguous access of 'member'
10+
> ambiguous access of 'member'
1211
13-
The member can derive from more than one object (it is inherited from more than one object). To resolve this error,
12+
A member is inherited from more than one base type, making unqualified access to that member ambiguous. To resolve this error:
1413

15-
- Make the member unambiguous by providing a cast.
16-
17-
- Rename the ambiguous members in the base classes.
14+
- Explicitly qualify access to the member.
15+
- Cast the object to the base class containing the member before accessing the member.
16+
- Rename the ambiguous member in the base class.
17+
- Bring the member into scope.
1818

1919
## Example
2020

21-
The following sample generates C2385.
21+
The following sample generates C2385:
2222

2323
```cpp
2424
// C2385.cpp
25-
// C2385 expected
26-
#include <stdio.h>
27-
2825
struct A
2926
{
30-
void x(int i)
31-
{
32-
printf_s("\nIn A::x");
33-
}
27+
void func1(int i) {}
28+
void func2() {}
3429
};
3530

3631
struct B
3732
{
38-
void x(char c)
39-
{
40-
printf_s("\nIn B::x");
41-
}
33+
void func1(char c) {}
34+
void func2() {}
4235
};
4336

44-
// Delete the following line to resolve.
45-
struct C : A, B {}
46-
47-
// Uncomment the following 4 lines to resolve.
48-
// struct C : A, B
49-
// {
50-
// using B::x;
51-
// using A::x;
52-
// };
37+
struct C : A, B
38+
{
39+
// Uncomment the following lines to resolve the first 2 errors
40+
// The error below for the call to c.func2() will remain
41+
// using A::func1;
42+
// using B::func1;
43+
};
5344

5445
int main()
5546
{
56-
C aC;
57-
aC.x(100);
58-
aC.x('c');
59-
}
47+
C c;
6048

61-
struct C : A, B
62-
{
63-
using B::x;
64-
using A::x;
65-
};
49+
c.func1(123); // C2385
50+
c.func1('a'); // C2385
51+
c.func2(); // C2385
52+
53+
c.A::func2(); // OK because explicitly qualified
54+
c.B::func2(); // OK because explicitly qualified
55+
static_cast<A>(c).func2(); // OK because of the cast
56+
static_cast<B>(c).func2(); // OK because of the cast
57+
}
6658
```
59+
60+
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:
61+
- Introduce the one you want to use into scope
62+
- Explicitly qualify the call with the base type
63+
- Cast the object before calling the function.

0 commit comments

Comments
 (0)