C++-结构在单独的文件中
C++ - Struct in separate file
我使用的是MVS 2013,我在文件ListStruct.h中编写了struct。在链接过程中,我收到错误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文件吗?它应该是什么样子?
在您没有显示的部分的标题ListStruct.h中似乎有构造函数的定义
ListStruct();
由于此标头包含在多个模块中,因此链接器会发出已定义构造函数的错误。
您应该只在一个模块中定义构造函数,或者在头中使用函数说明符inline
来定义它。
相关文章:
- 用C++在单独的头文件中完成函数体
- 如何使用单独文件中的派生类访问友元函数对象
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- 如何将 2 个类分成单独的 .h 文件并正确设置它们
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 从单独的文件中读取树
- 当结构位于单独的头文件中时'unregistered datatype'在 QML 中出现Q_GADGET错误
- 使用 Premake5 构建库,每个构建没有单独的项目文件
- 为什么要将函数声明和定义放在单独的文件中
- 使用 #include 将大量重复代码放入单独的文件中是否可以和/或正常?
- 创建单独的实现文件和头文件
- 在C++中对文件中的值单独执行计算
- 使用 ctest 的每个测试都需要单独的文件吗?
- 好友成员函数可以在单独的文件中使用吗?
- 运行单独的 cpp 文件,每个文件都有 main()
- 在 h 和 cpp 文件中单独声明和实现模板有时有效,有时会出现链接器错误
- 单独的类声明和方法定义文件问题
- 如何在C 中的Little Endian中读取来自二进制文件的单独的十六进制字符
- Howo 使用 cl 预处理为 masm 组装生成一个单独的文件
- 单独文件中各种 c++ 类的标头出现问题