在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
Getting struct redefinition error while using #pragma once in header and cpp files
Title.我在所有相关文件中使用过一次杂注,但仍然收到此错误。我将不胜感激任何帮助。
事件.h:
//This file is relevant to the others.
#pragma once
enum QueueType {Interactive, Noninteractive};
enum EventType {Start, Core, SSD, TTY};
struct Event {
//Event info
int time;
EventType e;
int PID;
QueueType q;
};
队列.h:
#pragma once
#include "Event.h"
//Problem code
struct nodeType {
Event info;
nodeType *link;
};
//Everything else in the file is basic Queue functions. Will provide if needed.
PriorityQueue.h:
#pragma once
#include "Event.h"
struct nodeType {
Event info;
nodeType *link;
};
//Also contains Priority Queue functions
主.cpp:
#include <iostream>
#include <string>
#include <assert.h>
#include "Event.h"
#include "Queue.h"
#include "PriorityQueue.h"
using namespace std;
//Also includes some test code
在 Queue.h 和 PriorityQueue.h 的实现文件中,我使用一次编译指示,并包含它们各自的头文件以及 iostream 和断言。我包括使用命名空间标准,因为我很懒。我所做的所有其他研究似乎都归结为不使用警卫的人,我认为这不是这里的问题。任何帮助将不胜感激。
您已经在两个不同的标头中定义了两次结构nodeType
。这将导致您的错误。仅在一个标头中定义它,或者如果它们是不同的类型,请为它们指定不同的名称或将它们放在单独的namespace
中。
#pragma once
或包含保护只能防止特定标头的多个包含;如果您以当前的方式违反了一个定义规则,则包含保护不会拯救您。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 禁止显示有关包含文件中 #pragma 包的警告
- 使用 #pragma 一次,#ifndef 在同一文件中包含保护
- 自动将 #pragma 一次添加到头文件
- 是否有充分的理由不在每个头文件中使用一次 #pragma
- 通过C++中的#pragma将.obj文件包含到Visual Studio中的项目中
- 使用GCC/GMake预编译头文件导致' pragma '警告(Premake)
- #pragma warning -文件特定的