无法在我的C++课中初始化 2d 矢量
Unable to initialize a 2d Vector in my C++ class
我已经阅读了我能找到的关于这个主题的所有帖子,但无法弄清楚我做错了什么。 我无法成功初始化我的 2d 向量,它是我类的成员变量。 头文件是:
class Beam2
{
private:
/*The following unit vectors should never be accessed directly
and are therefore private.*/
std::vector<std::vector<double> > unitVectors;
public:
//constructor
Beam2(
Node * node1PtrInput, Node * node2PtrInput,
double orientAngleInput);
我的 cpp 文件
Beam2::Beam2(
Node * node1PtrInput, Node * node2PtrInput, double orientAngleInput){
node1Ptr = node1PtrInput;
node2Ptr = node2PtrInput;
orientAngle = orientAngleInput;
unitVectors(3, std::vector<double>(3));
updateUnitVectors();
错误是:错误:对"(std::vector>) (int, std::vector)"的调用不匹配 unitVectors(3, std::vector(3)); ^任何帮助将不胜感激。
以下是初始化类的正确方法:
Beam2::Beam2(Node * node1PtrInput, Node * node2PtrInput, double orientAngleInput) :
node1Ptr(node1PtrInput),
node2Ptr(node2PtrInput),
orientAngle(orientAngleInput),
unitVectors(3, std::vector<double>(3))
{
updateUnitVectors(); // I believe this is function in the class
}
您也可以通过将unitVectors(3, std::vector<double>(3));
替换为unitVectors.resize(3, std::vector<double>(3));
来修复代码,但更喜欢前者。
相关文章:
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- 如何从initializer_list初始化 2D 数组
- 如何从文件中读取值,然后使用它们来初始化 2D 数组C++
- 为什么不能用两层列表初始值设定项初始化 2D std::array?
- 如何从嵌套的 std::initializer_list 初始化 2D C 样式数组?
- 从另一个类C++的构造函数初始化 2D 数组
- 在构造函数中初始化 2D 数组
- 如何初始化2D向量C
- 在类的构造函数中初始化 2D 向量
- 初始化2D矢量
- 无法解决动态初始化 2D 数组的问题
- 使用随机值初始化 2D 向量
- 使用多个std ::数组初始化2D std ::数组
- 如何使用初始值设定项列表初始化 2D 向量成员
- 如何在标头中声明后初始化 2D 字符数组
- 无法在我的C++课中初始化 2d 矢量
- 如何在 c++ 中使用构造函数初始化 2d 向量
- 我正在尝试创建一个函数来初始化 2D 数组C++
- 用于初始化 2D std::array 成员的初始值设定项列表
- 在c++中使用1 d数组初始化2d数组