我编写了一个类,在其中使用 typedef 定义一种新类型的列表,并在
private:
中初始化/声明一个向量。尽管包含了每个列表和向量的头文件,但列表和向量似乎都没有启动。当我在代码的不同部分引用任何一个时,它会返回错误'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();
/* functions for the class*/
private:
myList theList;
std::vector<MyServiceNamespace::aStruct> helperStructAttribute;
}; // class myAttributeClass
} // namespace myAttribute
(更改了变量名称,因为它们脱离源上下文没有意义)
我一直不知所措,因为这似乎是一个简单的修复,但是,我做了一些研究,但还没有找到答案。我希望另一双眼睛能帮助我看到森林中的树木。
我尝试过仅使用数组,但它遇到了与列表和向量相同的问题。我还尝试将列表声明为
typedef std::list<uint32_7>(myList)
但仍然返回错误 'myList' is not a member of 'myAttribute'
当我将向量的类型更改为
int
而不是在其他地方声明的结构时,向量工作正常。
编辑 我添加了更好的代码来支持这个问题(每个评论的人都是对的,我不够仔细,没有添加足够的信息)。 但是:我解决了我遇到的问题。该文件找不到 headerfile.h,因为包含路径有问题。虽然我很想知道这是如何导致 std::list 不起作用的,但我可能不会回答这个问题。感谢大家的快速回复和反馈。
这有效(参见(1)和(2)评论):
namespace my_service_namespace
{
using AStruct = struct SpecificStruct // <-- (1) 'USING' INSTEAD OF 'TYPEDEF'
{
double j{ 0 };
int c{ 0 };
};
/* Or:
struct AStruct
{
double j{ 0 };
int c{ 0 };
};
*/
class ServicesAttributes
{
public:
ServicesAttributes() {};
virtual ~ServicesAttributes() {};
int Add(const int a, const int b) { return a_ + b_; };
private:
int a_{ 0 }, b_{ 0 };
};
} // namespace my_service_namespace
#include <list>
#include <cstdint> // For uint32_t
#include <vector>
namespace my_attribute
{
class MyAttributeClass : public my_service_namespace::ServicesAttributes
{
public:
using MyList = std::list<uint32_t>; // <-- (2) 'USING' INSTEAD OF 'TYPEDEF'
MyAttributeClass() {};
virtual ~MyAttributeClass() {};
private:
MyList the_list_{};
std::vector<my_service_namespace::AStruct> helper_struct_attribute_{};
};
} // namespace my_attribute
int main()
{
my_attribute::MyAttributeClass my_attribute_class{};
}