[英]Accessing Bitset present inside a struct C++
I have a structure that looks like this :我有一个看起来像这样的结构:
struct bf_t {
bitset<250000> h0;
};
I want to know how to allocate memory for this struct and how to access h0 in my main() .我想知道如何为这个结构分配内存以及如何在 main() 中访问 h0 。
I tried doing this:我尝试这样做:
bf_t *b;
b->h0.set(1);
error: base operand of ‘->’ is not a pointer
bf_t *b ;
b.h0.set(1);
error: request for member ‘h0’ in ‘b’, which is of non-class type ‘long long int’
When you declare pointers like this bf_t *b;
当你声明这样的指针时
bf_t *b;
you have to assign to them before you use them.您必须在使用它们之前分配给它们。 You probably want to create a new bf_t on the heap like this:
b = new bf_t();
你可能想像这样在堆上创建一个新的 bf_t:
b = new bf_t();
. . Then you can use
->
to access its members.然后您可以使用
->
访问其成员。
The error message and your code mismatch.错误消息和您的代码不匹配。 However, it's much easier – you don't need pointers at all:
然而,这要容易得多——你根本不需要指针:
bf_t b;
b.h0.set(1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.