简体   繁体   English

C2572 - 当尝试在另一个文件中包含一个带有默认参数的函数,然后将此文件包含在 main 中时

[英]C2572 - When trying to include a function with a default parameter in another file, and then include this file in main

I am trying to build a small program in C++, to learn the preprocessor directives and how they actually work.我正在尝试用 C++ 构建一个小程序,以了解预处理器指令以及它们的实际工作方式。

The program is made up of 5 files: main.cpp , file1.h , file1.cpp , file2.h and file2.cpp该程序由 5 个文件组成: main.cppfile1.hfile1.cppfile2.hfile2.cpp

In file1.h , I declared 1 function that has the default parameter, and a new type byte:file1.h中,我声明了 1 个具有默认参数和新类型字节的函数:

typedef unsigned char byte;

byte f1(byte a, byte b = 5);

In file1.cpp , I defined it:file1.cpp中,我定义了它:

#include "file1.h"

byte f1(byte a, byte b) {

    return a + b;
}

In file2.h , I declared a second function that uses f1() , and always passes 10 to it as a second argument:file2.h中,我声明了第二个使用f1()的函数,并且始终将 10 作为第二个参数传递给它:

#include "file1.h"

byte f2(byte a);

And the same, in file2.cpp , I defined it:同样,在file2.cpp中,我定义了它:

#include "file2.h"

byte f2(byte a) {

    return f1(a, 10);
}

And finally, here is the main file:最后,这是主文件:

#include <iostream>
using namespace std;

#include "file1.h"

int main() {

    cout << f1(3) << endl;

    return 0;
}

For now, all is Ok, and the output is simply 8 .现在,一切正常,输出只是8

But suppose I need to use the f2() function in my main file, and for that I included the file2.h , so the main file is now:但是假设我需要在我的主文件中使用f2()函数,为此我包含了file2.h ,所以主文件现在是:

#include <iostream>
using namespace std;

#include "file1.h"
#include "file2.h"

int main() {

    cout << (int) f1(3) << endl;

    cout << (int) f2(2) << endl;

    return 0;
}

The compiler is giving this error: Error C2572 'f1': redefinition of default argument: parameter 1编译器给出此错误: Error C2572 'f1': redefinition of default argument: parameter 1

Since file1.h is included in file2.h , now f1() is redeclared in file2.h with the b paramter also set to 5 .由于file1.h包含在file2.h中,现在f1()file2.h中重新声明,并且b参数也设置为5

What can I do to prevent the redefinition, if we assume that I can not move the f2() declaration and definition to file1.h and file1.cpp respectively?如果我们假设我不能将f2()声明和定义分别移动到file1.hfile1.cpp ,我该怎么做才能防止重新定义?

Note: I know that I could use #pragma once directive, but I am trying to solve this problem without it, since I am trying to learn the C++ directives professionally.注意:我知道我可以使用#pragma once指令,但我试图在没有它的情况下解决这个问题,因为我正在尝试专业地学习 C++ 指令。

In the code shown, byte and f1() are being declared multiple times in main.cpp when file1.h and file2.h are both #include 'd.在显示的代码中,当file1.hfile2.h都被#include 'd 时, bytef1()main.cpp中被声明了多次。

Per the C++ standard, §8.3.6 [dcl.fct.default]/4 :根据 C++ 标准, §8.3.6 [dcl.fct.default]/4

[Note 2: A default argument cannot be redefined by a later declaration (not even to the same value) ( [basic.def.odr] ). [注意 2:默认参数不能由以后的声明重新定义(甚至不能重新定义为相同的值)( [basic.def.odr] )。 — end note] ——尾注]

Which is exactly what is happening here.这正是这里发生的事情。

Note: I know that I could use #pragma once directive, but I am trying to solve this problem without it, since I am trying to learn the C++ directives professionally.注意:我知道我可以使用#pragma once指令,但我试图在没有它的情况下解决这个问题,因为我正在尝试专业地学习 C++ 指令。

Making your .h files have proper header guards (see #pragma once vs include guards? ) is the correct and professional way to avoid redeclarations, eg:使您的.h文件具有适当的标头保护(参见#pragma once vs include guards? )是避免重新声明的正确且专业的方法,例如:

file1.h : file1.h

#ifndef File1H
#define File1H

typedef unsigned char byte;

byte f1(byte a, byte b = 5);

#endif

file2.h : file2.h

#ifndef File2H
#define File2H

#include "file1.h"

byte f2(byte a);

#endif

Online Demo在线演示

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

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