简体   繁体   English

[C]重新定义价值?

[英][C]redefinition of value?

I'm now working on a project and trying to organize the working code for later reproduction.我现在正在做一个项目,并试图组织工作代码以供以后复制。 And then I encountered the famous redefinition error and unfortunately, none of the answers hit the spot.然后我遇到了著名的重新定义错误,不幸的是,没有一个答案是当场的。 So I simplified my problem and here it is.所以我简化了我的问题,就是这样。

//main.c
#include <iostream>
#include "main.h"
#include "myfunc1.h"

int func2();
int func3();
int func4();

int main(int argc, char** argv) {
    
    func1();
    func2();
    func3();
    func4();
    
    return 0;
}

/* Trying to seperate func1*/

int func2(){
    int num2 = rep_val *2;
    int i = 0;
    for(i=0;i<num2;i++){
    printf("HO!\n");
    }
}
int func3(int rep){
    int num3 = rep_val *3;
    int i = 0;
    for(i=0;i<num3;i++){
    printf("HEY!\n");
    }
}
int func4(int rep){
    int num4 = rep_val *4;
    int i = 0;
    for(i=0;i<num4;i++){
    printf("WHAT?\n");
    }
}

So, all I want to do is to spread each func1,2,3,4 so main.c can remain clear.所以,我要做的就是传播每个func1,2,3,4以便main.c可以保持清晰。 And following main.h, myfunc1.h, myfunc.c are what I did for func1 .main.h, myfunc1.h, myfunc.c是我为func1所做的。

main.h主文件

int rep_val = 4;// myfunc1,2,3,4 will use this value so placed here

myfunc1.h myfunc1.h

#include "main.h"

int num1 = rep_val*2;//rep_val = 4

int func1();

myfunc1.cpp myfunc1.cpp

#include <myfunc1.h>

int func1(){
    int i = 0;
    for(i=0;i<num1;i++){
    printf("HI!\n");
    }
}

And the error was错误是

D:\DEV\DEV C\Header_practice\main.h [Error] redefinition of 'int rep_val'

What am I missing?我错过了什么? I've looked for several relevant queries and still can't find solution.我已经寻找了几个相关的查询,但仍然找不到解决方案。 Any word from you will be appriciated.你的任何一句话都会得到认可。 Thanks!谢谢!

Nam Jeongkuk南正国

The solution is to add a header guard to main.h and myfunc1.h and make variable definitions extern (turn them into declarations).解决方案是在main.hmyfunc1.h中添加一个 header 保护,并将变量定义为 extern(将它们转换为声明)。

main.h:主.h:

#ifndef __MAIN_H__
#define __MAIN_H__

extern int rep_val;

#endif

myfunc1.h: myfunc1.h:

#include "main.h"

#ifndef __MYFUNC1_H__
#define __MYFUNC1_H__

extern int num1;

int func1();

#endif

Alternatively, the #ifndef #define #endif sequences can be replaced by #pragma once with the most important compilers.或者, #ifndef #define #endif序列可以用最重要的编译器替换为#pragma once

And then you add the variable definitions to the corresponding c files.然后将变量定义添加到相应的 c 文件中。

main.c: main.c:

#include <iostream>
#include "main.h"
#include "myfunc1.h"

int rep_val = 4;// myfunc1,2,3,4 will use this value so placed here
...

myfunc1.c: myfunc1.c:

#include <myfunc1.h>

// Cannot initialize this variable with other
// variable value. Can only provide default.
// Be aware of race conditions!
int num1 = 0;

int func1(){
    // but can update this variable from within function
    num1 = rep_val*2; //rep_val = 4
    ...
}

However, it is not regarded as good practice to place variables in file scope if not really necessary.但是,如果不是真的需要,将变量放在文件 scope 中不被认为是一种好的做法。 Therefore i would suggest eg to put the num1 definition in the function func1 and remove it from the header at all.因此,我建议例如将num1定义放入 function func1并将其从 header 中删除。 You may think about your context and where to place your variable definitions.您可能会考虑您的上下文以及放置变量定义的位置。 If you have a good reason to provide a file scope variable then you'd go.如果您有充分的理由提供文件 scope 变量,那么您将提供 go。 The same may apply for rep_val .这同样适用于rep_val

First of all, the statement首先,声明

 int num1 = rep_val*2;

cannot reside on file scope, it has to be in a function scope (for the platform to know when to execute it).不能驻留在文件 scope 中,它必须位于 function scope 中(以便平台知道何时执行它)。 This instance of rep_val is seen as it's own definition, which conflict with the other one in the other translation unit.这个rep_val实例被视为它自己的定义,它与另一个翻译单元中的另一个冲突。

That said, without a header guard, you have multiple inclusion, which creates multiple definition of the same variable.也就是说,如果没有 header 保护,您就会有多个包含,这会创建同一变量的多个定义。 Do not define the variables in header files, declare them in header file and define them in one of the translation units.不要在 header 文件中定义变量,在 header 文件中声明它们并在翻译单元之一中定义它们。

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

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