我有一个纯 C++ 代码,有两个类:Dictionary 和 Training。我想使用这些类制作一个 WinForms 项目。所以,我有两种形式,需要将我的类成员共享给两者,就像全局变量一样。我试图以这种方式实现它,这是 MyForm.h 的一部分:
//MyForm.h , first(main form)
public ref class MyForm : public System::Windows::Forms::Form
{
private:
Dictionary *dict;
Training *train;
string *fileName;
public:
MyForm(void)
{
InitializeComponent();
dict = new Dictionary;
train = new Training;
fileName = new string;
}
有一些事件:
private: System::Void exploremanageToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
msclr::interop::marshal_context context;
ExploreForm^ eForm = gcnew ExploreForm(dict);
eForm->Show();
int rowCount;
vector<string> str;
str = dict->getAllWords(inverted);
eForm->DGV->RowCount = str.size();
for (int i = 0; i < str.size(); i++)
eForm->DGV->Rows[i]->Cells[0]->Value = context.marshal_as<String^>(str[i]);
eForm->buttonDelete->Enabled = false;
eForm->buttonEdit->Enabled = false;
eForm->textBoxEdit->Visible = false;
}
第二种形式的一部分:
//ExploreForm.h
public ref class ExploreForm : public System::Windows::Forms::Form
{
private: Dictionary *dict;
public:
ExploreForm(Dictionary *aDict)
{
InitializeComponent();
dict = aDict;
}
在所有标题中,我都有 #ifndef 或 #pragma 一次,但我仍然收到奇怪的 LNK2005 错误。
完整代码:
MyForm.h: https ://codeo.me/5mO
ExploreForm.h:https ://codeo.me/5mI
globals.h: https ://codeo.me/5mJ
globals.cpp: https ://codeo.me/5mK
Dictionary.h: https ://codeo.me/5mL
MyForm.cpp: https ://codeo.me/5mP
如何在两个表单之间共享我的本机 C++ 类成员?我知道,关于lnk2005有很多问题,但我真的有任何想法。