我的c++;正在删除向量元素 我是C++新手,正在制作2D游戏。 我似乎遇到了设置精灵动画的问题:

我的c++;正在删除向量元素 我是C++新手,正在制作2D游戏。 我似乎遇到了设置精灵动画的问题:,c++,memory-management,vector,C++,Memory Management,Vector,我有一个类,它包含一个精灵(sheet)动画数据的私有多维向量。该类的工作方式如下: #include <vector> class myClass { private: std::vector< std::vector<float> > BigVector; public: //constructor: fills the multidimentional vector //with one-dimentional vecto

我有一个类,它包含一个精灵(sheet)动画数据的私有多维向量。该类的工作方式如下:

#include <vector>

class myClass {

private:
    std::vector< std::vector<float> > BigVector;

public:
    //constructor: fills the multidimentional vector 
    //with one-dimentional vectors returned by myfunction.
    myClass() {

        //this line is called a few times within a while loop
        std::vector<float> holder = myFunction();

    }

    std::vector<float> myFunction() {

        std::vector<float> temp;
        //fill temp
        return temp;
    }

    //Other class access point for the vector
    float getFloat(int n, int m) {
        return Vector[n][m];
    }
};
然后在函数/构造函数中声明

Animator A(parameters); //creates a local instance of Animator called A
而不是

A = Animator(parameters); //redeclares A as a new Animator with the parameters
这就是我想要的。我的默认构造函数向BigVector添加了一个向量,使我认为BigVector的其余部分已被删除


希望这有帮助

我认为这只是一个输入错误,但应该是

float getFloat(int n, int m) {
   return BigVector[n][m];
}         ^^^
此外,您只需填充临时
持有者
向量,而从不将数据复制到
BigVector
。你应该改为:

myClass() 
{
   std::vector<float> holder = myFunction();
   BigVector.push_back(holder); // Put the newly filled vector in the multidimensional vector.
}
myClass()
{
std::vector holder=myFunction();
BigVector.push_back(holder);//将新填充的向量放入多维向量中。
}

如果可能的话,您可能希望使用引用,而不是按值复制。

感谢您的快速回复,Banex!Examle代码有点草率,我从您的指针中收集到:P@NyteQuist如果我的回答对你有帮助,你能接受吗?谢谢:)
myClass() 
{
   std::vector<float> holder = myFunction();
   BigVector.push_back(holder); // Put the newly filled vector in the multidimensional vector.
}