locked
redefinition error when usnig multiproject solution

    Question

  • Hi

    When I want to use some of my classes from another project in the same solution I get this error for my ref classes and enum classes

    error C2011: 'WindowsRuntimeComponent1::Class1' : 'class' type redefinition (MainPage.xaml.cpp) ...\app6\windowsruntimecomponent1\class1.h 7

    Steps to getting this error!

    1) create a blank windows store app( App6 )

    2) add a windows runtime component to the solution (WindowsRuntimeComponent1) It has already a class with the name of Class1

    3) now I want to use Class1 in Mainpage.xaml.cpp of App6 so I include Class1.h in mainpage.xaml.cpp and add this line to OnNavigatedTo 

    auto c = ref new WindowsRuntimeComponent1::Class1;

    4) for not getting Link error add reference to WindowsRuntimeComponent1 in App6

    The error is ready!

    I get 7 errors but I think the main is 'class' type redefinition.

    when I change Class1 to standard C++ class I get this link error

    unresolved external symbol "public: __thiscall WindowsRuntimeComponent1::Class1::Class1(void)" () referenced in function "protected: virtual void __cdecl App6::MainPage::[Windows::UI::Xaml::Controls::IPageOverrides]::OnNavigatedTo(class Windows::UI::Xaml::Navigation::NavigationEventArgs ^)" () 

    ...\App6\App6\MainPage.xaml.obj 

    And when I bring body of Class1::Class1() to Class1.h the app runs.

    I just want to use some of my C++ classes and ref classes in another project( because background task class should be in a separate project )

    Can anyone help me ?

    Thursday, October 25, 2012 6:42 AM

Answers

  • Hi,

    In this situation , you can not include the head file.
    First, add the reference.
    Then, use the namespace using namespace WindowsRuntimeComponent1; in you xaml.cpp file.
     
    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Hamed Hasani Saturday, October 27, 2012 9:15 PM
    Friday, October 26, 2012 2:46 AM

All replies

  • See the topics  i add in your other topic:

     Can Background Task class Be in the same project of the app?


    Sara Silva
    My Windows 8 Store Apps Samples
    Follow me in Twitter @saramgsilva

    My Windows 8 Store Apps: Female Pill | Galinho (Tic tac Toe) | 24
    (If my reply answers your question, please propose it as an answer because it will help other users)

    Thursday, October 25, 2012 10:53 PM
  • Hi,

    In this situation , you can not include the head file.
    First, add the reference.
    Then, use the namespace using namespace WindowsRuntimeComponent1; in you xaml.cpp file.
     
    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Hamed Hasani Saturday, October 27, 2012 9:15 PM
    Friday, October 26, 2012 2:46 AM
  • Hi Jesse

    As you said without including class1.h errors disappear and now I can use my ref classes. thank you

    but for my normal classes I get link error when body of a function is not in .h I think it is because linker can not find class1.cpp 

    I also try theses:

    • adding reference to WindowsRuntimeComponent1 in main project 
    • adding _declspec(dllexport) before definition and declaration of the function 
    • using static library and dll instead of windows runtime component

    but there is no effect

    what is the best way of using a normal class from another project in windows store apps?( #include "class1.cpp" works but I think it's not a good way)

    Friday, October 26, 2012 6:32 AM
  • I needed to divide my reusable C++ code that uses both WinRT types and Native types into two projects;

      • A static library project with /ZW for all functions and classes that use native types like std::wstring in public methods.
      • A Windows Runtime Component for all "public ref classes"

    And the reasons that require this division are:

    1. The compiler will raise an error if a Windows Runtime component has a type such as std::wstring as a return type or parameter in a public method.
    2. Authoring new WinRT components in a static library and linking + consuming them in another exe/dll is not a supported scenarios for VS 2012.
    3. The wmd format does not include any native types. You get a 'class' type redefinition error if you try to include a .h file.

    See also

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/269db513-64ef-4817-a025-43954f614eb3/#12ae4229-c8c8-494e-8e14-3eaea3c0f21c

    I believe there is a large gap in the VS2012 documentation on the subject of how to use C++ libraries with Windows Store Apps.

    Friday, October 26, 2012 1:51 PM
  • Thanks for your help Andrew I tried it but it became so confusing to handle ( because I also have another projects for my background task ) and I couldn't prevent getting errors and warnings

    finally I solve the problem by using ref classes instead of my reusable C++ classes.

    I also think there must be more documentation about different kind of projects, their options and restriction in using them.

    Saturday, October 27, 2012 9:15 PM