|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: Compiler Error C2385"
|
3 | 3 | title: "Compiler Error C2385"
|
4 |
| -ms.date: "11/04/2016" |
| 4 | +ms.date: "1/19/2024" |
5 | 5 | f1_keywords: ["C2385"]
|
6 | 6 | helpviewer_keywords: ["C2385"]
|
7 |
| -ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417 |
8 | 7 | ---
|
9 | 8 | # Compiler Error C2385
|
10 | 9 |
|
11 |
| -ambiguous access of 'member' |
| 10 | +> ambiguous access of 'member' |
12 | 11 |
|
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: |
14 | 13 |
|
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. |
18 | 18 |
|
19 | 19 | ## Example
|
20 | 20 |
|
21 |
| -The following sample generates C2385. |
| 21 | +The following sample generates C2385: |
22 | 22 |
|
23 | 23 | ```cpp
|
24 | 24 | // C2385.cpp
|
25 |
| -// C2385 expected |
26 |
| -#include <stdio.h> |
27 |
| - |
28 | 25 | struct A
|
29 | 26 | {
|
30 |
| - void x(int i) |
31 |
| - { |
32 |
| - printf_s("\nIn A::x"); |
33 |
| - } |
| 27 | + void func1(int i) {} |
| 28 | + void func2() {} |
34 | 29 | };
|
35 | 30 |
|
36 | 31 | struct B
|
37 | 32 | {
|
38 |
| - void x(char c) |
39 |
| - { |
40 |
| - printf_s("\nIn B::x"); |
41 |
| - } |
| 33 | + void func1(char c) {} |
| 34 | + void func2() {} |
42 | 35 | };
|
43 | 36 |
|
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 | +}; |
53 | 44 |
|
54 | 45 | int main()
|
55 | 46 | {
|
56 |
| - C aC; |
57 |
| - aC.x(100); |
58 |
| - aC.x('c'); |
59 |
| -} |
| 47 | + C c; |
60 | 48 |
|
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 | +} |
66 | 58 | ```
|
| 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