如何从类/构造函数中初始化对象的2d向量
How to actually initialize a 2d vector of objects from a class/constructor
到目前为止,唯一接近回答这个问题的链接是:我如何初始化一个stl向量的对象本身有非平凡的构造函数?
然而,我试着去做,但我仍然被它难住了。
相关代码:
边缘// Edge Class
class Edge{
public:
// std::string is used to avoid not a name type error
Edge (std::string, double);
double get_dist();
std::string get_color();
~Edge();
private:
std::string prv_color; // prv_ tags to indicate private
double prv_distance;
};
Edge::Edge (std::string color, double distance){
prv_color = color;
prv_distance = distance;
};
图// Graph Class
class Graph{
public:
Graph (double, double);
double get_dist_range();
~Graph();
private:
double prv_edge_density; // how many edges connected per node
double prv_dist_range; // start from 0 to max distance
std::vector < std::vector <Edge*> > nodes; // the proper set-up of
};
// Graph constructor
Graph::Graph (double density, double max_distance){
prv_edge_density = density;
prv_dist_range = max_distance;
nodes (50, std::vector <Edge*> (50)); // THIS LINE STUMPS ME MOST
};
当我试图初始化对象指针的向量时,我从下面的行中得到了这个错误:
nodes (50, std::vector <Edge*> (50)); // Error at this line
error: no match for call to ‘(std::vector<std::vector<Edge*, std::allocator<Edge*> >,
std::allocator<std::vector<Edge*, std::allocator<Edge*> > > >)
(int, std::vector<Edge*, std::allocator<Edge*> >)’
我想尽快得到关于这件事的建议。
注意:假设我使用了。cpp文件和。h文件来分隔代码你需要了解初始化列表
// Graph constructor
Graph::Graph (double density, double max_distance) :
nodes (50, std::vector <Edge*> (50))
{
prv_edge_density = density;
prv_dist_range = max_distance;
}
未测试的代码。
相关文章:
- 用C++中的CPerson(类)类型的对象初始化STL矢量
- 对象初始化中是否允许指向此成员的指针?
- 对象初始化后在C++中显示 char 数组时的异常行为
- 为什么两种不同的对象初始化方式给出不同的输出
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- afxmem.cpp中的对象初始化差异
- 删除通过取消引用新对象初始化的对象
- 如何使用sregex_token_iterator对象初始化向量
- 关于默认构造函数,对象初始化/使用C++ OOP
- C++ 基元类型初始化与对象初始化
- 哪个函数负责C++全局范围内的类对象初始化?
- C++中构造函数的对象初始化出现问题
- 如何修复模板 BST 类的对象初始化
- 无法在 QML/C++ 中使用绑定对象初始化 UI
- 为什么参数可以在对象初始化时通过赋值运算符传递给构造函数?
- 类对象初始化的二维向量
- GCC:当层次结构中存在虚拟继承时,C++11 内联对象初始化(使用 "this")不起作用
- 对象初始化
- 类对象初始化
- 使用从另一个类继承的类的对象初始化成员对象