[英]Accessing Bitset present inside a struct C++
我有一個看起來像這樣的結構:
struct bf_t {
bitset<250000> h0;
};
我想知道如何為這個結構分配內存以及如何在 main() 中訪問 h0 。
我嘗試這樣做:
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’
當你聲明這樣的指針時bf_t *b;
您必須在使用它們之前分配給它們。 你可能想像這樣在堆上創建一個新的 bf_t: b = new bf_t();
. 然后您可以使用->
訪問其成員。
錯誤消息和您的代碼不匹配。 然而,這要容易得多——你根本不需要指針:
bf_t b;
b.h0.set(1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.