From 25222b7647bf4f86b55a1bcebc0e5243f6d60e3b Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 20 Dec 2023 20:54:11 +0800 Subject: [PATCH 1/2] Revamp C2385 --- .../compiler-errors-1/compiler-error-c2385.md | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/docs/error-messages/compiler-errors-1/compiler-error-c2385.md b/docs/error-messages/compiler-errors-1/compiler-error-c2385.md index 36b74b59467..48b09665e11 100644 --- a/docs/error-messages/compiler-errors-1/compiler-error-c2385.md +++ b/docs/error-messages/compiler-errors-1/compiler-error-c2385.md @@ -1,7 +1,7 @@ --- description: "Learn more about: Compiler Error C2385" title: "Compiler Error C2385" -ms.date: "11/04/2016" +ms.date: "12/20/2023" f1_keywords: ["C2385"] helpviewer_keywords: ["C2385"] ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417 @@ -10,57 +10,52 @@ ms.assetid: 6d3dd1f2-e56d-49d7-865c-6a9acdb17417 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, +The member can be inherited from more than one base type causing an unqualified access to be ambiguous. To resolve this error: -- Make the member unambiguous by providing a cast. +- Make the member unambiguous by explicitly qualifying it, or providing a cast. - Rename the ambiguous members in the base classes. +- Bring the desired member into scope. + ## Example -The following sample generates C2385. +The following sample generates C2385: ```cpp // C2385.cpp -// C2385 expected -#include - 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 + // 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 + c.B::func2(); // OK + static_cast(c).func2(); // OK + static_cast(c).func2(); // OK +} ``` + +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. From 8bb356c91f908fba41bd116ee1ebc82e0c220bfd Mon Sep 17 00:00:00 2001 From: Tyler Whitney Date: Fri, 19 Jan 2024 13:23:40 -0800 Subject: [PATCH 2/2] Update compiler-error-c2385.md I like what you did here. Did a little word smithing on the overall topic. --- .../compiler-errors-1/compiler-error-c2385.md | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/error-messages/compiler-errors-1/compiler-error-c2385.md b/docs/error-messages/compiler-errors-1/compiler-error-c2385.md index 48b09665e11..073c282ce9b 100644 --- a/docs/error-messages/compiler-errors-1/compiler-error-c2385.md +++ b/docs/error-messages/compiler-errors-1/compiler-error-c2385.md @@ -1,22 +1,20 @@ --- description: "Learn more about: Compiler Error C2385" title: "Compiler Error C2385" -ms.date: "12/20/2023" +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 be inherited from more than one base type causing an unqualified access to be ambiguous. 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 explicitly qualifying it, or providing a cast. - -- Rename the ambiguous members in the base classes. - -- Bring the desired member into scope. +- 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 @@ -39,6 +37,7 @@ struct B 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; }; @@ -49,13 +48,16 @@ int main() c.func1(123); // C2385 c.func1('a'); // C2385 - c.func2(); // C2385 - c.A::func2(); // OK - c.B::func2(); // OK - static_cast(c).func2(); // OK - static_cast(c).func2(); // OK + + c.A::func2(); // OK because explicitly qualified + c.B::func2(); // OK because explicitly qualified + static_cast(c).func2(); // OK because of the cast + static_cast(c).func2(); // OK because of the cast } ``` -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. +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.