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

问题:

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

[plain] view plaincopyprint?
  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中,

[cpp] view plaincopyprint?
  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中定义的顺序不一致。

将其中的两行

[cpp] view plaincopyprint?
  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) { }  
改为
[cpp] view plaincopyprint?
  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) { }  
重新编译,问题解决。

[plain] view plaincopyprint?
  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  

你可能感兴趣的:(嵌入式 解决编译warning:warning: ‘MeteringUnit::voltage_gain_’ will be initialized after [-Wreorder])