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.