locked
Static property LNK2005

    Question

  • Hi, please, can someone explain me why I'm getting a linker error LNK2005 ?

    it's an example, I dont want to use a static variable, enum or a literal constant, I want a static property.

    // xxx.h

    public ref class Colors sealed
    {

    public :

        static property int Violet { int get( ); }
    };

    // xxx.cpp

    int Colors::Violet::get( )
    {
        return 0xEE82EE;
    }

    // main.cpp

    #include "xxx.h"

    ////////////////////////////

    error LNK2005: "public: int __cdecl XXX::__ColorsActivationFactory::Violet::XXX::__IColorsStatics[::Violet]::get(void)" (?get@?

    IViolet@__IColorsStatics@XXX@1__ColorsActivationFactory@3@Q$AAAHXZ) already defined in Main.obj

    • Edited by t-n-x Saturday, January 7, 2012 12:11 PM
    Saturday, January 7, 2012 12:09 PM

Answers

All replies

  • Please submit a bug on this if you haven't already here. Let me know if you have trouble filing it please.

    Thanks,

    -David

    Thursday, January 12, 2012 3:00 AM
    Moderator
  • Can you check whether xxx.cpp file is part of your project? If it isn't, its output (xxx.obj) won't get linked in the final binary and that's the error you should be getting.

    Otherwise, it might be a bug as David mentioned, but one that I am not able to repro with more recent builds.

    Thanks,
    Marian
    Visual C++

    Monday, January 16, 2012 6:39 PM

  • Thanks for yours answers.. yes xxx.cpp file is part of my project and linked into the final output.

    Can someone reproduce this bug or is it a syntax error ?

    I start a new DirectXApplication project, I add two new items, xxx.h and xxx.cpp (VS 2011 Express Dev Preview).


    //xxx.h
    #pragma once

    namespace XXX
    {
        public ref class MyClass sealed
        {

        public :

            static property int MyStaticProperty { int get( ); }
            property int MyProperty { int get( ); }
        };
    }


    // xxx.cpp
    #include "pch.h"
    #include "xxx.h"

    namespace XXX
    {
        int MyClass::MyStaticProperty::get( )
        {
            return 1;
        }

        int MyClass::MyProperty::get( )
        {
            return 1;
        }
    }


    //main.cpp
    #include "pch.h"
    #include "ViewProvider.h"

    #include "xxx.h"
    using namespace XXX;

    [Platform::MTAThread]
    int main(array<Platform::String^>^)
    {
        MyClass^ myclass = ref new MyClass( );

        int x = MyClass::MyStaticProperty; // LNK2005
        int y = myclass->MyProperty; // OK

        auto viewProvider = ref new ViewProviderFactory();
        Windows::ApplicationModel::Core::CoreApplication::Run(viewProvider);
    }

    //Error LNK2005: "public: int __cdecl XXX::__MyClassActivationFactory::MyStaticProperty::XXX::__IMyClassStatics[::MyStaticProperty]::get(void)" //(?get@?IMyStaticProperty@__IMyClassStatics@XXX@1__MyClassActivationFactory@3@Q$AAAHXZ) already defined in Main.obj




    Tuesday, January 17, 2012 3:28 PM
  • The syntax is correct. It is a codegen bug in the //BUILD/ bits.

    Wednesday, January 18, 2012 8:53 PM