全局变量定义, error C2374: 'g_aaaaa' : redefinition; multiple initialization

本文介绍了如何正确地在C++项目中使用全局变量。主要内容包括全局变量的最佳实践,即在CPP文件中定义,并通过EXTERN关键字在其他需要使用的文件中声明。文章还特别强调了EXTERN声明时不应再次初始化变量,避免重复定义错误。

全局变量最好在cpp里面定义,

然后如果其他的文件用到该变量的时候,加个extern,但是注意 extern  的时候,不能再对变量初始化。

 

例如:

A.cpp 中定义 DWORD g_aaaaa = 0;

在B.cpp 文件中,=如果使用,则 extern DWORD g_aaaaa;

 

如果不小心写成 DWORD g_aaaaa = 0;

则会提示  重复定义

error C2374: 'g_aaaaa' : redefinition; multiple initialization
;

Build target 'Target 1' compiling main.c... main.c(3): error C141: syntax error near 'void' main.c(4): error C141: syntax error near '{' main.c(5): error C141: syntax error near '{' main.c(6): error C231: 'P2_4': redefinition main.c(6): error C231: 'P2_4': redefinition main.c(6): error C231: 'P2_3': redefinition main.c(6): error C231: 'P2_3': redefinition main.c(6): error C231: 'P2_2': redefinition main.c(6): error C231: 'P2_2': redefinition main.c(6): error C141: syntax error near 'break' main.c(7): error C279: 'P2_4': multiple initialization main.c(7): error C231: 'P2_4': redefinition main.c(7): error C231: 'P2_4': redefinition main.c(7): error C279: 'P2_3': multiple initialization main.c(7): error C231: 'P2_3': redefinition main.c(7): error C231: 'P2_3': redefinition main.c(7): error C279: 'P2_2': multiple initialization main.c(7): error C231: 'P2_2': redefinition main.c(7): error C231: 'P2_2': redefinition main.c(7): error C141: syntax error near 'break' main.c(8): error C279: 'P2_4': multiple initialization main.c(8): error C231: 'P2_4': redefinition main.c(8): error C231: 'P2_4': redefinition main.c(8): error C279: 'P2_3': multiple initialization main.c(8): error C231: 'P2_3': redefinition main.c(8): error C231: 'P2_3': redefinition main.c(8): error C279: 'P2_2': multiple initialization main.c(8): error C231: 'P2_2': redefinition main.c(8): error C231: 'P2_2': redefinition main.c(8): error C141: syntax error near 'break' main.c(9): error C279: 'P2_4': multiple initialization main.c(9): error C231: 'P2_4': redefinition main.c(9): error C231: 'P2_4': redefinition main.c(9): error C279: 'P2_3': multiple initialization main.c(9): error C231: 'P2_3': redefinition main.c(9): error C231: 'P2_3': redefinition main.c(9): error C279: 'P2_2': multiple initialization main.c(9): error C231: 'P2_2': redefinition main.c(9): error C231: 'P2_2': redefinition main.c(9): error C141: syntax error near 'break' main.c(10): error C279: 'P2_4': multiple initialization main.c(10): error C231: 'P2_4': redefinition main.c(10): error C231: 'P2_4': redefinition main.c(10): error C279: 'P2_3': multiple initialization main.c(10): error C231: 'P2_3': redefinition main.c(10): error C231: 'P2_3': redefinition main.c(10): error C279: 'P2_2': multiple initialization main.c(10): error C231: 'P2_2': redefinition main.c(10): error C231: 'P2_2': redefinition main.c(10): error C141: syntax error near 'break' main.c(11): error C279: 'P2_4': multiple initialization main.c(11): error C231: 'P2_4': redefinition main.c(11): error C231: 'P2_4': redefinition main.c(11): error C279: 'P2_3': multiple initialization main.c(11): error C231: 'P2_3': redefinition main.c(11): error C231: 'P2_3': redefinition main.c(11): error C279: 'P2_2': multiple initialization main.c(11): error C231: 'P2_2': redefinition main.c(11): error C231: 'P2_2': redefinition main.c(11): error C141: syntax error near 'break' main.c(12): error C279: 'P2_4': multiple initialization main.c(12): error C231: 'P2_4': redefinition main.c(12): error C231: 'P2_4': redefinition main.c(12): error C279: 'P2_3': multiple initialization main.c(12): error C231: 'P2_3': redefinition main.c(12): error C231: 'P2_3': redefinition main.c(12): error C279: 'P2_2': multiple initialization main.c(12): error C231: 'P2_2': redefinition main.c(12): error C231: 'P2_2': redefinition main.c(12): error C141: syntax error near 'break' main.c(13): error C279: 'P2_4': multiple initialization main.c(13): error C231: 'P2_4': redefinition main.c(13): error C231: 'P2_4': redefinition main.c(13): error C279: 'P2_3': multiple initialization main.c(13): error C231: 'P2_3': redefinition main.c(13): error C231: 'P2_3': redefinition main.c(13): error C279: 'P2_2': multiple initialization main.c(13): error C231: 'P2_2': redefinition main.c(13): error C231: 'P2_2': redefinition main.c(13): error C141: syntax error near 'break' main.c(15): error C231: 'P0': redefinition main.c(15): error C202: 'Number': undefined identifier main.c(15): error C231: 'P0': redefinition main.c(16): error C141: syntax error near '}' Target not created. Build Time Elapsed: 00:00:01这个错误怎么改?
10-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值