无法在我的C++课中初始化 2d 矢量

Unable to initialize a 2d Vector in my C++ class

本文关键字:初始化 2d 矢量 C++ 我的      更新时间:2023-10-16

我已经阅读了我能找到的关于这个主题的所有帖子,但无法弄清楚我做错了什么。 我无法成功初始化我的 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));来修复代码,但更喜欢前者。