我几个月来一直在学习C ++ / CLI,但无论我尝试什么,我似乎无法解决我遇到的问题。
我需要将一个String ^,Array ^或一个ArrayList ^数据类型从main.cpp传递给Form1.h。我试图在main.cpp中创建变量global,然后使用extern调用变量。但是,这不适用于String,Array和ArrayList数据类型。
我该怎么做呢?提前致谢。以下是我的代码的解释:
//main.cpp
bool LoadFileFromArgument = false; //A boolean can be declared global
String^ argument; //this will not pass from main.cpp to Form1.h
int main(array<System::String ^> ^args)
{
String ^ argument = args[1]
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
//Form1.h
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
extern bool LoadFileFromArgument; //is grabbed without error
extern String^ argument; //will not work
这是错误:
error C3145: 'argument' : global or static variable may not
have managed type 'System::String ^'
may not declare a global or static variable,
or a member of a native type that refers to objects in the gc heap
答案 0 :(得分:2)
您是否可以为表单创建重载的构造函数。即。
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(String^ argument)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
// Use "argument" parameter as req'd.
}
Form1(void)
{
//....usual constructor here...
//..etc...
然后从主
// Create the main window and run it
Application::Run(gcnew Form1(argument));