初始化std::list/std::vector失败及'myList'非成员错误排查
问题分析:头文件路径错误引发的STL容器初始化问题
我定义了一个类,用typedef声明std::list<uint32_t>为myList,并在private区域声明了std::vector<MyServiceNamespace::aStruct>类型的成员。尽管已包含对应头文件,list和vector仍无法正常初始化,其他代码引用时会报错:'myList' is not a member of 'myAttribute'。
相关代码
头文件headerfile.h
namespace MyServiceNamespace { struct { double j; int c; } aStruct; class ServicesAttributes { public: ServicesAttributes(); virtual ~ServicesAttributes(); int madeAdd(int a, int b) {return a + b; }; private: int a, b; }; }
MyAttribute类文件
#include <headerfile.h> #include <list> #include <vector> namespace myAttribute { class myAttributeClass : public MyServiceNamespace::ServicesAttributes { public: typedef std::list<uint32_t> myList; myAttributeClass(); virtual ~myAttributeClass(); /* 类成员函数*/ private: myList theList; std::vector<MyServiceNamespace::aStruct> helperStructAttribute; }; // class myAttributeClass } // namespace myAttribute
排查过程
- 尝试过改用数组、修改
typedef声明方式,均无效 - 将
vector类型改为int时,代码可正常工作 - 最终发现问题出在
headerfile.h的包含路径错误,但不清楚为何该错误会导致std::list无法正常使用
内容的提问来源于stack exchange,提问作者pnwdino
相关产品推荐
相关产品推荐

