嵌入式 解决编译warning:warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder]

本文介绍了一个关于C++构造函数成员变量初始化顺序的问题及解决方案。在Ubuntu 12.04环境下使用g++ 4.6.3编译时出现了关于变量初始化顺序的警告,通过调整构造函数中成员变量的初始化顺序解决了该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

环境:ubuntu 12.04,g++版本4.6.3,编译目标文件时出现warnings:

  1. u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/src/trunk$ make clean;make  
  2. rm -f *.o local_ctrl  
  3. g++ -g3 -Wall -o0 -c msgrcv_cmd.cpp -o msgrcv_cmd.o  
  4. In file included from msgrcv_cmd.h:24:0,  
  5.                  from msgrcv_cmd.cpp:30:  
  6. controller.h: In constructor ‘MeteringUnit::MeteringUnit(size_t, double, double, double)’:  
  7. controller.h:92:12: warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder]  
  8. controller.h:91:12: warning:   ‘double MeteringUnit::current_gain_’ [-Wreorder]  
  9. controller.h:77:5: warning:   when initialized here [-Wreorder]  
  10. g++ -g3 -Wall -o0 -c controller.cpp -o controller.o  
  11. In file included from controller.cpp:21:0:  
  12. controller.h: In constructor ‘MeteringUnit::MeteringUnit(size_t, double, double, double)’:  
  13. controller.h:92:12: warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder]  
  14. controller.h:91:12: warning:   ‘double MeteringUnit::current_gain_’ [-Wreorder]  
  15. controller.h:77:5: warning:   when initialized here [-Wreorder]  
  16. g++ -g3 -Wall -o0 -c thread.cpp -o thread.o  
  17. g++ -g3 -Wall -o0 -c ini_file.cpp -o ini_file.o  
  18. g++ -g3 -Wall -o0 -c main_ctrl.cpp -o main_ctrl.o  
  19. In file included from main_ctrl.cpp:25:0:  
  20. controller.h: In constructor ‘MeteringUnit::MeteringUnit(size_t, double, double, double)’:  
  21. controller.h:92:12: warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder]  
  22. controller.h:91:12: warning:   ‘double MeteringUnit::current_gain_’ [-Wreorder]  
  23. controller.h:77:5: warning:   when initialized here [-Wreorder]  

解决办法:

1. 出问题的地方在头文件controller.h中,

  1. class MeteringUnit {  
  2. public:  
  3.     MeteringUnit(size_t port_num = 1, double pgain = 1.0, double cgain = 1.0, double vgain = 1.0)  
  4.     : port_(port_num), power_gain_(pgain), voltage_gain_(vgain), current_gain_(cgain) { }  
  5.     ~MeteringUnit();  
  6.     void Refresh();  
  7.     double Power() const;  
  8.     double Current() const;  
  9.     double Voltage() const;  
  10. private:  
  11.     size_t port_;  
  12.     operation* mu_op_;  
  13.     static const SensorType sensor_typ_ = EMETER;  
  14. private:  
  15.     static const int emeter_pulse_const_ = 3200;  
  16.     double power_gain_;  
  17.     double current_gain_;  
  18.     double voltage_gain_;  
  19.     double power_;  
  20.     double current_;  
  21.     double voltage_;  
  22.     int gpqs1_; // GP1/GQ1/GS1(0x50/0x51/0x52)  
  23.     int gphs1_; // Gphs1(0x6d)  
  24.     int p1offset_;  // P1offset(0x65)  
  25. };  
从编译后的提示,已经可以很明白地看出错在什么地方了,
MeteringUnit::voltage_gain_应该在double MeteringUnit::current_gain_之后初始化。

也就是说,构造函数中变量初始化的顺序与该成员变量在类MeteringUnit中定义的顺序不一致。

将其中的两行

  1.     MeteringUnit(size_t port_num = 1, double pgain = 1.0, double cgain = 1.0, double vgain = 1.0)  
  2.     : port_(port_num), power_gain_(pgain), voltage_gain_(vgain), current_gain_(cgain) { }  
改为
  1. MeteringUnit(size_t port_num = 1, double pgain = 1.0, double cgain = 1.0, double vgain = 1.0)  
  2. : port_(port_num), power_gain_(pgain), current_gain_(cgain), voltage_gain_(vgain) { }  
重新编译,问题解决。

  1. u1204@u1204-zhw:~/hwsvn/2sw/4prj_mips/UCP_rt5350/src/trunk$ make clean;make  
  2. rm -f *.o local_ctrl  
  3. g++ -g3 -Wall -o0 -c msgrcv_cmd.cpp -o msgrcv_cmd.o  
  4. g++ -g3 -Wall -o0 -c controller.cpp -o controller.o  
  5. g++ -g3 -Wall -o0 -c thread.cpp -o thread.o  
  6. g++ -g3 -Wall -o0 -c ini_file.cpp -o ini_file.o  
  7. g++ -g3 -Wall -o0 -c main_ctrl.cpp -o main_ctrl.o  
  8. g++ -o local_ctrl msgrcv_cmd.o controller.o thread.o ini_file.o main_ctrl.o -L../../drivers -lphysicalop -lpthread  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值