g++出现In instantiation of ...required from here ...warning可能的的解决方法

本文探讨了C++中构造函数初始化列表的一个警告问题,指出初始化列表顺序应与类内变量声明顺序一致,否则可能导致意外的执行顺序和潜在错误。举例说明了错误的初始化顺序可能引发的内存安全问题,并强调了良好编码习惯的重要性。

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

  • 头一次碰到g++出现了一个很奇怪的警告,输出信息大概是 : In instantiation of …required from here …warning
    在这里插入图片描述

  • 改来改去改不掉,最终发现是因为构造函数初始化列表没有按照类内声明变量的顺序来写,比如

class A {
public:
    A(int a, char b) :m_b(b),  m_a(a) { }  // warning
    
private:
    int m_a;
    char m_b;
}

// 改成下面这样就没警告了
class A {
public:
    A(int a, char b) : m_a(a), m_b(b) { }  // no warning
    
private:
    int m_a;
    char m_b;
}
  • 看起来无厘头的警告其实是有道理的。经查证,初始化列表执行的顺序与书写初始化列表时的顺序无关,和在类内声明变量的顺序一样。产生警告就是告诉我们,这里可能与我们书写的顺序不一样,在一个成员变量的初始化与另一个成员变量有关时,可能造成BUG。比如:
class String {
public:
    String(const char * str) : m_str(str), m_len(std::strlen(m_str))  { }
private:
    uint64_t m_len;
    char m_str;
}
  • 上面的程序可能出现不符合预期的结果, 甚至Crash, 因为虽然m_len好像在m_str构造后构造, 但是实际上是在其前构造, 相当于对一个野指针调用strlen, 后果不用多说。
  • 养成习惯 :初始化列表按顺序书写初始化列表内不要利用一个成员变量去构造另一个
[1/3] Building CXX object .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj FAILED: .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj "D:\Program Files (x86)\mingw64\bin\g++.exe" @.llt\src_llt\sd8911\hidsp0\testcase\alg\CMakeFiles\src_llt-sd8911-hidsp0-testcase-alg.dir\odsp_test_tx_fde.cpp.obj.rsp -MD -MT .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj -MF .llt\src_llt\sd8911\hidsp0\testcase\alg\CMakeFiles\src_llt-sd8911-hidsp0-testcase-alg.dir\odsp_test_tx_fde.cpp.obj.d -o .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj -c ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp In file included from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.hpp:10, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:27: D:/8911/hlt/thirdparty/googletest/googletest-release-1.11.0/googletest/include/gtest/gtest.h:1928: warning: "FAIL" redefined In file included from D:/8911/src/cbb/base/common.h:12, from D:/8911/hlt/src_llt/common/llt_common.hpp:18, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:25: D:/8911/src/cbb/base/error_code.h:144: note: this is the location of the previous definition ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:41:1: error: expected initializer before 'using' compilation terminated due to -Wfatal-errors. [2/3] Building CXX object .llt/src_llt/sd8911/hidsp0/testcase/CMakeFiles/src_llt-sd8911-hidsp0-testcase.dir/llt_ecs_msa.cpp.obj In file included from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/types/AnyCast.h:25, from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/IsEqual.h:21, from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ChainingMockHelper.h:22, from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/mockcpp.hpp:23, from D:/8911/hlt/comm/llt_support/llt_head_files.h:25, from D:/8911/hlt/comm/llt_support/llt_support.h:11, from ../../../src_llt/sd8911/hidsp0/testcase/llt_ecs_msa.cpp:1: D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/Ignore.h: In member function 'bool mockcpp::Ignore::operator==(const mockcpp::Ignore&) const': -- D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookHolderFactory.h:32:14: required from 'static mockcpp::ApiHookHolder* mockcpp::ApiHookHolderFactory::create(F*) [with F = unsigned char()]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookMocker.h:33:48: required from 'mockcpp::InvocationMockBuilderGetter mockcpp::mockAPI(const string&, API*) [with API = unsigned char(); std::__cxx11::string = std::__cxx11::basic_string<char>]' ../../../src_llt/sd8911/hidsp0/testcase/llt_ecs_msa.cpp:568:5: required from here D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:67:33: warning: unused parameter 'api' [-Wunused-parameter] D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h: In instantiation of 'static bool mockcpp::ApiHookGenerator<F, 0>::freeApiHook(void*) [with F = unsigned char()]': D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:53:49: recursively required from 'static bool mockcpp::ApiHookGenerator<F, Seq>::freeApiHook(void*) [with F = unsigned char(); unsigned int Seq = 9]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:53:49: required from 'static bool mockcpp::ApiHookGenerator<F, Seq>::freeApiHook(void*) [with F = unsigned char(); unsigned int Seq = 10]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ParameterizedApiHookHolder.h:44:47: required from 'mockcpp::ParameterizedApiHookHolder<F>::~ParameterizedApiHookHolder() [with F = unsigned char()]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ParameterizedApiHookHolder.h:42:4: required from here D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:82:35: warning: unused parameter 'hook' [-Wunused-parameter] ninja: build stopped: subcommand failed. [build] make build end. For more detailed information, please check ./BUILD.LOG. Build Fail! 什么问题
最新发布
07-27
h:81:16: warning: catching polymorphic type ‘struct pangolin::BadInputException’ by value [-Wcatch-value=] /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h: In instantiation of ‘void pangolin::VarWrapper<T, S>::Set(const T&) [with T = bool; S = float]’: /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:76:10: required from here /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:81:16: warning: catching polymorphic type ‘struct pangolin::BadInputException’ by value [-Wcatch-value=] /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h: In instantiation of ‘void pangolin::VarWrapper<T, S>::Set(const T&) [with T = bool; S = long int]’: /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:76:10: required from here /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:81:16: warning: catching polymorphic type ‘struct pangolin::BadInputException’ by value [-Wcatch-value=] /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h: In instantiation of ‘void pangolin::VarWrapper<T, S>::Set(const T&) [with T = bool; S = int]’: /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:76:10: required from here /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:81:16: warning: catching polymorphic type ‘struct pangolin::BadInputException’ by value [-Wcatch-value=] /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h: In instantiation of ‘void pangolin::VarWrapper<T, S>::Set(const T&) [with T = bool; S = short int]’: /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:76:10: required from here /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:81:16: warning: catching polymorphic type ‘struct pangolin::BadInputException’ by value [-Wcatch-value=] /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h: In instantiation of ‘void pangolin::VarWrapper<T, S>::Set(const T&) [with T = bool; S = bool]’: /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:76:10: required from here /home/lh/business_logic_modules/mobile_robot_platform/ros2_ws/src/calibration_cameraimu/../../../third_party/x86_64_22.04/Pangolin/include/pangolin/var/varwrapper.h:81:16: warning: catching polymorphic type ‘struct pangolin::BadInputException’ by value [-Wcatch-value=] gmake[2]: *** [CMakeFiles/calibration_cameraimu.dir/build.make:104:CMakeFiles/calibration_cameraimu.dir/src/vins_estimator/src/core/CameraIMUCalibration.cpp.o] 错误 1 gmake[2]: *** [CMakeFiles/calibration_cameraimu.dir/build.make:90:CMakeFiles/calibration_cameraimu.dir/src/vins_estimator/src/core/calibra_camera_imu.cpp.o] 错误 1 gmake[1]: *** [CMakeFiles/Makefile2:137:CMakeFiles/calibration_cameraimu.dir/all] 错误 2 gmake: *** [Makefile:146:all] 错误 2
07-04
这段代码中出现了两个错误,都是因为operator<<函数中的参数类型为const LinkedList<T>,而在这个函数中调用了LinkedList类的非const成员函数,导致编译器报错。 第一个错误是在调用size函数的时候出现的,因为size函数不是const成员函数,而operator<<函数的参数类型为const LinkedList<T>,无法调用非const成员函数。解决办法是将size函数定义为const成员函数。 第二个错误是在调用operator[]函数的时候出现的,同样是因为operator[]函数不是const成员函数。解决办法是将operator[]函数定义为const成员函数,以便在const对象上也可以调用。 修改后的代码如下: ``` #include <iostream> using namespace std; template <typename T> class LinkedList { template <typename U> friend ostream& operator << (ostream& out, const LinkedList<U>& ll); struct Node { T data; Node* next; Node(T data = T()) : data(data), next(nullptr) {} } *head = new Node(); public: LinkedList() {} void append(T data) { Node* ptr = head; while (ptr -> next != nullptr) ptr = ptr -> next; ptr -> next = new Node(data); return; } T& operator [] (int index) const { Node* ptr = head -> next; int length = 0; while (length != index) { ptr = ptr -> next; length ++; } return ptr -> data; } int size() const { Node* ptr = head; int length = 0; while (ptr -> next != nullptr) { ptr = ptr -> next; length ++; } return length; } }; template <class T> ostream& operator << (ostream& out, const LinkedList<T>& ll) { out << "[ "; for (int _ = 0; _ < ll.size(); _ ++) { out << ll[_] << ' '; } out << ']'; return out; } int main() { LinkedList<int> l = LinkedList<int>(); l.append(10); l.append(100); l.append(1000); cout << l << endl; return 0; } ``` 经过修改后,程序可以正确地输出链表中的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值