gpt4 book ai didi

c++ - LNK2005,C++/CLI,不知道

转载 作者:行者123 更新时间:2023-11-30 00:50:37 25 4
gpt4 key购买 nike

我有一个纯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;
}

在所有 header 中,我都有一次#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

字典.h:https://codeo.me/5mL

MyForm.cpp:https://codeo.me/5mP

如何在两种形式之间共享我的 native C++ 类成员?我知道,有很多关于lnk2005的问题,但我真的有什么想法。

最佳答案

您正在头文件中定义您的方法。当您将该头文件包含在多个翻译单元中时,这意味着有多个定义。这就是链接器提示的内容:

.... already defined ....

将方法的定义从 .h 文件移到 .cpp 文件中。

关于c++ - LNK2005,C++/CLI,不知道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24430301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com