Variables in c++ builder XE2 not initializing when unit was called by component

社会主义新天地 提交于 2020-01-06 07:24:20

问题


I have a unit (WebFunctions.h) with the declaration

String RawURLAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~.-_";

This unit works well in the app. But when I add a component which also declares WebFunctions.h, the initialization of RawURLAllowedChars does not occurs (RawURLAllowedChars = NULL).

To get the app back to work, plus remove the class declaration of the component is still necessary delete the WebFunctions.obj file.

Note: Any declarations in .cpp file, with or without a extern declaration in .h file, also are not working.

This is a bug in XE2 or I'm missing something? Thanks.

Obs.: Var declarations inside #ifndef .. #endif


回答1:


Do not initialize a global variable in a header file. Declare the variable as extern in the .h file and then initialize it in the corresponding .cpp file, eg:

WebFunctions.h:

extern const String RawURLAllowedChars;

WebFunctions.cpp:

const String RawURLAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~.-_";



回答2:


This is more a workaround than a real solution... but...

Comment (in the .cpp file of main app) the line with #pragma link "Component", added with the component which uses WebFunctions.h (not initializing unit).

Delete all building files in Debug\Win32 folder.

Recompile the App.



来源:https://stackoverflow.com/questions/16095768/variables-in-c-builder-xe2-not-initializing-when-unit-was-called-by-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!