You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中指向结构体指针向量的指针赋值代码报错问题咨询

Fixing the Pointer Dereference Error in Your Struct/Vector Code

Let's break down exactly why that line is failing, fix it, and also point out some potential pitfalls you might run into with this approach.

The Root Cause of the Error

Your problematic line is:

(*keys_)[size_]->(*value)[size] = inputValue; //此行报错

Here's what's wrong with the syntax:

  1. (*keys_)[size_] correctly gives you an info* (a pointer to your info struct instance).
  2. To access the value member of that info struct, you need to use ->value (since you're working with a pointer to info). This gives you a std::vector<int*>*—another pointer, this time pointing to a vector of integer pointers.
  3. The invalid part is (*value): value is a member name of the info struct, not a standalone pointer variable. You can't dereference it directly like that. Instead, you need to first get the pointer to the vector via the info struct, then dereference that pointer to access the vector's elements.

The Fixed Code

Here's the corrected line:

(*(*keys_)[size_]->value)[size] = inputValue;

Or, to make the code much more readable (and easier to debug), split it into separate variables:

// Get the current info pointer from the keys vector
info* currentInfo = (*keys_)[size_];
// Get the pointer to the value vector from the info struct
std::vector<int*>* valueVector = currentInfo->value;
// Dereference the vector pointer and assign the value
(*valueVector)[size] = inputValue;

Important Additional Notes

While fixing the syntax will resolve the immediate error, there are some critical issues with this code structure you should address:

  • Massive Memory Leak Risk: You're using raw pointers everywhere (new std::vector<info*>, new info, new std::vector<int*>) without any corresponding delete calls. All this dynamically allocated memory will never be freed, leading to memory leaks. Consider switching to smart pointers like std::unique_ptr or std::shared_ptr to manage memory automatically, or better yet, use value types instead of pointers where possible (e.g., change std::vector<int*>* value to std::vector<int*> value unless you have a specific reason to use a pointer).
  • No Boundary Checking: Using [] to access vector elements doesn't do any bounds checking. If size_ exceeds the size of *keys_, or size exceeds the size of *valueVector, you'll get undefined behavior (crashes, corrupted data, etc.). Use vector::at() instead if you want explicit out-of-bounds exceptions, or add your own checks before accessing elements.
  • Unnecessary Pointer Complexity: Most of these pointers are likely unnecessary. For example, keys_ could be a std::vector<info> (or std::vector<std::unique_ptr<info>>) instead of a pointer to a vector of pointers. Reducing unnecessary indirection will make your code simpler, safer, and easier to maintain.

内容的提问来源于stack exchange,提问作者bgk1293

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:52:20