locked
Problem with purely virtual method in abstract class in Metro style Windows Runtime Component

    Question

  • Hi,

    When developing a Windows Runtime Component in Visual Studio 2012 RC (penultimate template under new  project | Templates | Visual C++ | Windows Metro Style), using a Xaml client, based on the 'Blank App (XAML) template I get the following error:

    error C2253: 'CppWindowsRuntimeComponent::AbstractBase::GetHelloWorld' : pure specifier or abstract override specifier only allowed on virtual function (MainPage.xaml.cpp)

    The following code generates the error, when consumed in the client:

    Component code

    Consumption of the component in MainPage.cpp is as follows (after having referenced the component):

    Client code

    Can anyone please tell me what's wrong? I don't seem to get past this one.

    Regards,

    Marc.

    Monday, June 11, 2012 7:39 PM

Answers

  • Hi Marc,

    According to the language architect, a public abstract class isn't a valid WinRT concept. This will be reflected in a compiler error message in the next public release of the C++/CX compiler. In other words, your WinRT component will fail to build. The interface is the way to go here if this type must be public.

    • Marked as answer by OlManMarc Saturday, June 16, 2012 6:53 AM
    Friday, June 15, 2012 11:05 PM
    Moderator

All replies

  • I can't reproduce the error, I get a clean compile with your code under 2012 RC

    Can you add the /Bv compiler switch in the "command line", "additional options" and post the versions emitted in the output window under compiler passes?

    Monday, June 11, 2012 10:08 PM
    Moderator
  • Hi Steve,

    As requested, I rebuild the solution with the compiler switch in the client project (the component project builds fine). Here's the build log output:

    Build log output

    Hopefully this is meaningful to you.

    I am, of course interested to learn how you got the code to compile.

    Regards,

    Marc.

    Tuesday, June 12, 2012 5:56 PM
  • Ok, those versions look fine.

    I didn't read closely and pull out that this requires two projects. After separating into a component project and a Blank Xaml consumer, I can reproduce this. My non-repro was all in one project, relying on header includes instead of winmd imports

    You can workaround by converting your abstract class to an interface

    //abstractbase.h

    public interface class IAbstractBase { String^ GetHelloWorld(); };


    // class1.h
    public ref class Class1 sealed : public IAbstractBase
    {
    public:
       Class1(void);
       virtual String^ GetHelloWorld();
    };

    Though it seems your code should work. I've pinged some folks to confirm if this is a bug.

    Thursday, June 14, 2012 8:34 PM
    Moderator
  • Hi Steve,

    Thank you for your workaround. Good to have this option.

    Please do post some text covering the reactions of the people you pinged. If this is not a bug, then what is the intended practice?

    Regards,

    Marc. 

    Friday, June 15, 2012 6:44 AM
  • Hi Marc,

    According to the language architect, a public abstract class isn't a valid WinRT concept. This will be reflected in a compiler error message in the next public release of the C++/CX compiler. In other words, your WinRT component will fail to build. The interface is the way to go here if this type must be public.

    • Marked as answer by OlManMarc Saturday, June 16, 2012 6:53 AM
    Friday, June 15, 2012 11:05 PM
    Moderator
  • Hi Steve,

    Thanks you for clearing this up. I now know what to do.

    Regards,

    Marc.

    Saturday, June 16, 2012 6:53 AM