I’m trying to debug a program in C++.
For some reason, I’m unable to see the contents of a std::vector of pointers in the VSCode debugger. The vector appears in the debugger, but I can’t inspect the actual objects or their details. How can I resolve this and view the contents clearly?
This is my code:
#include <iostream>
#include <memory>
#include <vector>
class MyClass {
public:
MyClass(int id) : id(id) {}
void display() const { std::cout << "Object ID: " << id << std::endl; }
private:
int id;
};
int main() {
std::vector<std::shared_ptr<MyClass>> myObjects;
for (int i = 1; i <= 7; ++i) {
myObjects.push_back(std::make_shared<MyClass>(i));
}
for (const auto& obj : myObjects) {
obj->display();
}
return 0;
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/my_user/project/build/test",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
And this is what I see, when I’m trying to examine the vector of pointers:
I compile in debug mode:
cmake .. -DCMAKE_BUILD_TYPE=Debug
I can see the id of only the first element in the vector.
How can I view the full contents of a std::vectorstd::shared_ptr<MyClass> clearly in the VSCode debugger?