如何在其构造函数中将对象(的指针)添加到向量中?

How to add (pointer of) an object to vector in its constructor?

#include <string>
#include <vector>
using namespace std;
struct object{
    int value;
    string name;
    object(string str, int val):name(str), value(val){
        objList.push_back(this);
    }
};

vector<object*> objList;

我想在创建对象时添加对象的指针,但程序报错:"Use of undeclared identifier 'objList'",我将objList的声明移到对象的定义上,它给出了警告:"Use of undeclared identifier 'object'" .如何在创建对象时添加对象指针?

尝试以下方法

using namespace std;

vector<struct object*> objList;

struct object{
    int value;
    string name;
    object(string str, int val): value(val), name(str) {
        objList.push_back(this);
    }
};

using namespace std;

struct object;

vector<object*> objList;

struct object{
    int value;
    string name;
    object(string str, int val): value(val), name(str) {
        objList.push_back(this);
    }
};