简体   繁体   English

基本C ++重新定义错误

[英]Basic C++ Redefinition Error

I've been reading about header guards and and their use to solve redefinition errors, but I'm not quite sure how to implement it properly. 我一直在阅读有关标头保护以及它们用于解决重定义错误的信息,但是我不确定如何正确实现它。 Here's an simplified example of what I'm trying to do: 这是我要执行的操作的简化示例:

fileA.h 文件A.h

#include first_header.h
#include second_header.h

// code here

fileB.h 文件B.h

#include first_header.h
#include second_header.h

// code here

mainfile.cpp mainfile.cpp

#include fileA.h
#include fileB.h

// code here

Now the problem comes in the mainfile.cpp since I need to include both the fileA.h and fileB.h headers, but each of these contains the same header references thus giving me the redefinition error. 现在问题出在mainfile.cpp中,因为我需要同时包含fileA.h和fileB.h标头,但是每个标头都包含相同的标头引用,因此给了我重新定义错误。 I'm not really sure how to get around it or implement the header guards properly in this scenario. 我不太确定在这种情况下如何解决它或正确实现标头防护。

Logic: Check if specific macro is defined, if the macro is not defined, define it and include the content of the header file. 逻辑:检查是否定义了特定的宏,如果未定义该宏,则定义它并包括头文件的内容。 This will prevent from duplicated inclusion of the header content. 这样可以防止重复包含标题内容。

Like this: 像这样:

file A: 档案A:

#ifndef fileA_h
#define fileA_h

#include first_header.h
#include second_header.h

//code here

#endif

file B: 文件B:

#ifndef fileB_h
#define fileB_h

#include first_header.h
#include second_header.h

//code here

#endif

First of all, you need to put either quotes or angle brackets around the filenames, like this: #include <fileA.h> or #include "fileA.h" 首先,您需要在文件名两边加上引号或尖括号,例如: #include <fileA.h>#include "fileA.h"

From what you've posted, it looks like you don't really understand how header guards work. 从您发布的内容来看,您似乎不太了解标题保护的工作原理。 So here's the rundown. 所以这是总结。

Let's say I have a function that I want to be able to call from different c++ files. 假设我有一个函数,希望能够从不同的c ++文件中调用。 You first need a header file. 您首先需要一个头文件。 (You can do includes from inside your include guard.) (您可以从包含防护中进行包含。)

#ifndef MYHEADER_HPP
#define MYHEADER_HPP

#include "first_header.h"
#include "second_header.h"

void MyFunction();

#endif

The non-include preprocessor lines make up what is called an "Include Guard," and you should always guard your header files. 非包含预处理程序行构成了所谓的“包含保护”,因此您应始终保护头文件。

You then implement said function in a .cpp file, like this: 然后,您可以在.cpp文件中实现上述功能,如下所示:

#include "MyHeader.hpp"

void MyFunction()
{
    // Implementation goes here
}

Now, you can use this function in other code: 现在,您可以在其他代码中使用此功能:

#include "MyHeader.hpp"

int main()
{
    MyFunction();
}

If you're using an IDE compiling and linking is handled for you, but just in case, you can compile and link like this using g++ (assuming a "main.cpp" file and a "MyFunction.cpp" file): 如果您使用的是IDE,则可以为您处理编译和链接,但以防万一,您可以使用g ++(假设有“ main.cpp”文件和“ MyFunction.cpp”文件)进行编译和链接:

g++ main.cpp -c
g++ MyFunction.cpp -c
g++ main.o MyFunction.o -o MyExecutable

I hope this helps and good luck! 我希望这个帮助能祝你好运!

Most compilers (eg Visual Studio) also support #pragma once which can be used instead of include guards expressed as "#ifndef". 大多数编译器(例如Visual Studio)也支持#pragma once它可以代替包含表示为“ #ifndef”的防护使用。 You will find that in some legacy code written with MS Visual C++. 您会在用MS Visual C ++编写的一些旧代码中发现这一点。

However, the 但是,那

#ifndef MYHEADER_HPP
#define MYHEADER_HPP

// code here

#endif

is more portable. 更便携。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM