1
我是新來的C++,並在製作2D遊戲。 我似乎遇到了動畫我的精靈的問題:我的C++向量元素被刪除
我有一個類,其中包含一個精靈(表)的動畫數據的私人多維向量。類的工作是這樣的:
#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];
}
};
這個類本身是另一個類,檢索使用getFloat函數包含的數據。
在構造函數的最後,BigVector充滿了一些包含浮動的向量,因爲它應該是。但是,當構造函數退出並且我想使用getFloat函數檢索數據時,BigVector只包含1個元素;第一個被添加的元素。
我認爲這與持有人向量超出範圍有關... 任何想法?
編輯
我已經找到了答案:錯不在這一類,但與使用它的類: 相反的(再)宣佈我的私人「動畫師」,我聲明的局部變量,這阻止了我的動畫師更新。 Basicly:
private: Animator A //calls upon the default construstor of Animator class
然後在函數/構造函數聲明的
A = Animator(parameters); //redeclares A as a new Animator with the parameters
Animator A(parameters); //creates a local instance of Animator called A
,而不是這是我想要的東西。我的默認構造函數向BigVector添加了一個向量,導致我認爲BigVector的其餘部分被刪除。
希望這會有所幫助!
感謝您的快速回復,Banex! 從我的指針中收集到的示例代碼有點草率:P – NyteQuist
@NyteQuist如果它對您有幫助,您是否願意接受我的答覆作爲答案?謝謝 :) – Banex