This is a C++ version of a duplicate question. I know the pointers to vectors a bit much in the following, but I did this in order to duplicate a much larger project. The members of info_a are not properly printing from within print_b and I have been unable to properly pass the vector of pointers.
With what I have tried, the members of info_a are not properly printing from within print_b. The first element is fine, but the next two are not.
The structs and print_b are from a third-party api and I am trying to pass in what they are expecting.
Here's the working code ...
http://coliru.stacked-crooked.com/a/c3ad6af6da9409a5
Does anyone see where I am going wrong?
typedef struct {
uint16_t a1;
uint8_t a2;
uint8_t a3;
} info_a;
typedef struct {
uint16_t id;
unsigned int arr_sz;
info_a *arr;
} info_b;
void print_a(const info_a* a)
{
using namespace std;
cout <<
"a->a1 0x" << hex << setfill('0') << setw(4) << a->a1 << std::endl <<
"a->a2 0x" << hex << setfill('0') << setw(2) << a->a2 << std::endl <<
"a->a3 0x" << hex << setfill('0') << setw(2) << a->a3 << std::endl;
}
void print_b(const info_b* b)
{
using namespace std;
cout << "b->id 0x" << hex << setfill('0') << setw(4) << b->id << endl
<< "b->arr_sz " << hex << setfill('0') << setw(2) << b->arr_sz << endl;
for (unsigned int i = 0; i < b->arr_sz; ++i) {
const info_a *elem = &(b->arr[i]);
print_a(elem);
}
}
int main(int argc, char **argv)
{
std::shared_ptr<std::vector<std::shared_ptr<info_a>>> sp_info_a =
std::make_shared<std::vector<std::shared_ptr<info_a>>>();
std::shared_ptr<std::vector<std::shared_ptr<info_b>>> sp_info_b =
std::make_shared<std::vector<std::shared_ptr<info_b>>>();
int offset = 0;
for (uint16_t i = 1; i <= 1; ++i) {
for (uint16_t j = 1; j <= 3; ++j) {
std::shared_ptr<info_a> a_info =
std::make_shared<info_a>(info_a { j, 0x31, 0x32 } );
sp_info_a->push_back(a_info);
}
std::shared_ptr<info_b> b_info = std::make_shared<info_b>(
info_b {
static_cast<uint16_t>(i),
static_cast<unsigned int>(sp_info_a->size()),
(*sp_info_a)[offset].get()
});
sp_info_b->push_back(b_info);
print_b(b_info.get());
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire