C++ - 结构在单独的文件中
C++ - Struct in separate file
我正在使用 MVS 2013,我在文件 ListStruct.h 中编写了结构。在链接期间我收到错误 LNK2005:
error LNK2005: "public: __thiscall ListStruct::ListStruct(void)" (??0ListStruct@@QAE@XZ) already defined in projekt1.obj
现在 - ListStruct.h
的一部分
#ifndef _LISTSTRUCT_H_
#define _LISTSTRUCT_H_
#include "stdafx.h"
struct ListStruct{
Member *head; //wskaznik na poczatek listy
Member *tail; //wskaznik na koniec listy
void AddMember(int value);
void RemoveMember(int value);
void Display();
ListStruct();
};
#endif
我的部分主要内容:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
ListStruct *base = new ListStruct;
system("pause");
return 0;
}
我做错了什么?我必须创建 ListStruct.cpp 文件吗?它应该是什么样子?
似乎在header ListStruct.h中你没有显示的部分有构造函数的定义
ListStruct();
由于此 header 包含在多个模块中,因此链接器会发出构造函数已定义的错误。
要么只在一个模块中定义构造函数,要么在 header.
中用函数说明符 inline
定义它
我正在使用 MVS 2013,我在文件 ListStruct.h 中编写了结构。在链接期间我收到错误 LNK2005:
error LNK2005: "public: __thiscall ListStruct::ListStruct(void)" (??0ListStruct@@QAE@XZ) already defined in projekt1.obj
现在 - ListStruct.h
的一部分#ifndef _LISTSTRUCT_H_
#define _LISTSTRUCT_H_
#include "stdafx.h"
struct ListStruct{
Member *head; //wskaznik na poczatek listy
Member *tail; //wskaznik na koniec listy
void AddMember(int value);
void RemoveMember(int value);
void Display();
ListStruct();
};
#endif
我的部分主要内容:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
ListStruct *base = new ListStruct;
system("pause");
return 0;
}
我做错了什么?我必须创建 ListStruct.cpp 文件吗?它应该是什么样子?
似乎在header ListStruct.h中你没有显示的部分有构造函数的定义
ListStruct();
由于此 header 包含在多个模块中,因此链接器会发出构造函数已定义的错误。
要么只在一个模块中定义构造函数,要么在 header.
中用函数说明符inline
定义它