如何在对象的构造函数中添加(指针)到矢量
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;
我想在创建对象时添加对象的指针,但程序给出错误:"使用未声明的标识符'objList'",我将 objList 的声明移到对象的定义上,它给出警告:"使用未声明的标识符'对象'"。创建对象时如何添加对象的指针?
尝试以下操作
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);
}
};
相关文章:
- 是否允许向空指针添加零?
- 将指向结构数组的指针添加到 std::map
- 将 char* 推送到矢量时出现问题,但在每次迭代后,它会将指向相同值缓冲区的指针添加到矢量中
- 将对象指针添加到向量后的值更改
- 将指针添加到成员对象中的指针动态阵列
- 需要一些关于使用指针添加C++数组的建议
- 如何将函数指针添加到现有va_list
- 为什么对象属性的值在 c++ 中将其指针添加到映射后返回零
- 将对象指针添加到位于类内部的指向对象的指针向量
- 无法将对象上的指针添加到集合
- 将指针添加到不同命名空间中的类时出错
- 向无符号字符指针添加短int
- Delphi中的类型转换指针添加
- 如何将节点指针添加到矢量指针
- 我可以向数组指针添加元素吗
- 向指针添加"const"可以帮助优化吗?
- 向函数指针添加constness时会发生什么
- 向指针添加偏移量
- 为什么使用std::next而不是向指针添加整数
- 无法将指针添加到类指针向量数组