LNK2001 and constants

This is a discussion on LNK2001 and constants within the C++ Programming forums, part of the General Programming Boards category; Hi guys. Got meself a LNK2001 error here. I prolly know the cause is this: Trying to use a global ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    LNK2001 and constants

    Hi guys. Got meself a LNK2001 error here. I prolly know the cause is this:

    Trying to use a global constant in C++ in multiple files can cause LNK2001. In C++, unlike C, global constants have static linkage. To get around this limitation, you can include the const initializations in a header file and include that header in your .cpp files, or you can make the variable non-constant and use a constant reference to access it.
    Now for the case, I put the constants on a file that will be accessed by many other files because it contains the main global class (or you can call it the main singleton class). The file is called display.h

    Code:
    // BAWE-20100119: ATTRACT_MOVIE_FOR_MULTI_MODE. Movie filename constants
    const LPCWSTR ATTRACT_MOVIE_FILENAME[5] = {
    	L"..\\_resource\\attractmovies\\001ATTRACT.avi", //Default attract movie. Always the first one.
    	L"..\\_resource\\attractmovies\\002QUICKPIC.avi", //QUICK MODE
    	L"..\\_resource\\attractmovies\\003FUNPIC.avi", //FUN MODE
    	L"..\\_resource\\attractmovies\\004THEMEPIC.avi", //THEME MODE
    	L"..\\_resource\\attractmovies\\005SEXYPIC.avi" //HOT MODE
    
    };
    // END BAWE-20100119: ATTRACT_MOVIE_FOR_MULTI_MODE. Movie filename constants
    
    // BAWE-20100209: ATTRACT_MOVIE_FOR_MULTI_MODE_2. Demo movie filename constants
    const LPCWSTR DEMO_MOVIE_FILENAME[4] = {
    	L"..\\_resource\\attractmovies\\QuickPickDemoscreen.avi", //QUICK MODE
    	L"..\\_resource\\attractmovies\\FunPickDemoscreen.avi", //FUN MODE
    	L"..\\_resource\\attractmovies\\ThemePickDemoscreen.avi", //THEME MODE
    	L"..\\_resource\\attractmovies\\SexyPickDemoscreen.avi" //HOT MODE
    
    };
    // END BAWE-20100209: ATTRACT_MOVIE_FOR_MULTI_MODE_2. Demo movie filename constants
    And there are 2 other classes and files that use these constants. One is InitLayer in layer_init.cpp and DemoScreen in layer_demo.cpp. Thus, when I tried to build the application, it displayed LNK2001 errors like so:

    Error 728 error LNK2001: unresolved external symbol "private: static wchar_t const * const * const DemoScreen:EMO_MOVIE_FILENAME" (?DEMO_MOVIE_FILENAME@DemoScreen@@0QBQB_WB) layer_demo.obj
    Error 729 error LNK2001: unresolved external symbol "private: static wchar_t const * const * const DemoScreen::ATTRACT_MOVIE_FILENAME" (?ATTRACT_MOVIE_FILENAME@DemoScreen@@0QBQB_WB) layer_demo.obj
    I was thinking of duplicating those constants to 2 other constants in both layer_init.cpp and layer_demo.cpp but that is a bad programming practice. If I were to change them, I must change all of those constants so sooner or later it might become another bug if the value of the constants and the duplicates aren't the same. How do I solve this problem? Thanks.
    Last edited by g4j31a5; 03-01-2010 at 08:07 PM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Nevermind, I've solved it myself. Damn, this is all just another false alarm. It's due to my own clumsiness. There are these lines in the DemoScreen declaration:

    Code:
        protected:
    		static const LPCWSTR ATTRACT_MOVIE_FILENAME[5]; // BAWE-20100119: ATTRACT_MOVIE_FOR_MULTI_MODE.
    		static const LPCWSTR DEMO_MOVIE_FILENAME[4]; // BAWE-20100209: ATTRACT_MOVIE_FOR_MULTI_MODE_2. DEMO_MOVIE_FILENAME constant
    FYI, the constants were actually only being used by the DemoScreen so I put them there in the first place. But now, one more object, the InitLayer, is using them so I moved the constants to another place that is accessible everywhere. Damn, it took me 1 whole day to figure out this stupid mistake. :P

    Consider the case solved. Thanks anyway guys for listening to my rants.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21