Skip to content

Commit 25222b7

Browse files
authored
Revamp C2385
1 parent c009e1d commit 25222b7

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed
Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: Compiler Error C2385"
33
title: "Compiler Error C2385"
4-
ms.date: "11/04/2016"
4+
ms.date: "12/20/2023"
55
f1_keywords: ["C2385"]
66
helpviewer_keywords: ["C2385"]
77
ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417
@@ -10,57 +10,52 @@ ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417
1010

1111
ambiguous access of 'member'
1212

13-
The member can derive from more than one object (it is inherited from more than one object). To resolve this error,
13+
The member can be inherited from more than one base type causing an unqualified access to be ambiguous. To resolve this error:
1414

15-
- Make the member unambiguous by providing a cast.
15+
- Make the member unambiguous by explicitly qualifying it, or providing a cast.
1616

1717
- Rename the ambiguous members in the base classes.
1818

19+
- Bring the desired member into scope.
20+
1921
## Example
2022

21-
The following sample generates C2385.
23+
The following sample generates C2385:
2224

2325
```cpp
2426
// C2385.cpp
25-
// C2385 expected
26-
#include <stdio.h>
27-
2827
struct A
2928
{
30-
void x(int i)
31-
{
32-
printf_s("\nIn A::x");
33-
}
29+
void func1(int i) {}
30+
void func2() {}
3431
};
3532

3633
struct B
3734
{
38-
void x(char c)
39-
{
40-
printf_s("\nIn B::x");
41-
}
35+
void func1(char c) {}
36+
void func2() {}
4237
};
4338

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-
// };
39+
struct C : A, B
40+
{
41+
// Uncomment the following lines to resolve the first 2 errors
42+
// using A::func1;
43+
// using B::func1;
44+
};
5345

5446
int main()
5547
{
56-
C aC;
57-
aC.x(100);
58-
aC.x('c');
59-
}
48+
C c;
6049

61-
struct C : A, B
62-
{
63-
using B::x;
64-
using A::x;
65-
};
50+
c.func1(123); // C2385
51+
c.func1('a'); // C2385
52+
53+
c.func2(); // C2385
54+
c.A::func2(); // OK
55+
c.B::func2(); // OK
56+
static_cast<A>(c).func2(); // OK
57+
static_cast<B>(c).func2(); // OK
58+
}
6659
```
60+
61+
Ambiguous calls to `func1` can be resolved by bringing both overloads into scope. However, doing the same for `func2` does not fix the error, since both `A::func2` and `B::func2` take no arguments and are still ambiguous. If only one is desired, introducing just one into scope does fix the issue. Alternatively, the call can be explicitly qualified with the base type, or the object can be casted before the function is called.

0 commit comments

Comments
 (0)