作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个纯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/
这个问题在这里已经有了答案: How to get all enum values in Java? (8 个回答) 关闭5年前。 我想创建一个 JComboBox 来处理选择给它的任何 Enum。为
我是一名优秀的程序员,十分优秀!