讓我解釋一下這種情況:無法獲得的另一種載體內從矢量數據
我有一個類cAnimation有幾個方法
#include "SDL/SDL.h"
#include <vector>
#include <fstream>
using namespace std;
class cAnimation{
private:
vector<SDL_Rect> frames;
public:
cAnimation();
void setQntFrames(int n){
this->frames.resize(n);
ofstream log("qntframes.txt");
log << "capacity = " << this->frames.capacity();
}
void setFrame(int index,int x, int y, int w, int h){
this->frames[index].x = x;
this->frames[index].y = y;
this->frames[index].w = w;
this->frames[index].h = h;
ofstream log("setrect.txt");
log << "i = " << i
<< "x = " << this->frames.at(i).x
<< "y = " << this->frames.at(i).y
<< "w = " << this->frames.at(i).w
<< "h = " << this->frames.at(i).h;
}
SDL_Rect cAnimation::getFrame(int index){
return this->frames[index];
}
};
我在我的main.cpp這樣做(在包括都行)
vector<cAnimation> animation;
animation.resize(1);
animation[0].setQntFrames(10); // it's printing the right value on qntframes.txt
animation[0].setFrame(0,10,10,200,200) // it's printing the right values on setrect.txt
SDL_Rect temp = animation[0].getFrame(0);// here is the problem
ofstream log("square.txt");
log << "x = " << temp.x
<< "y = " << temp.y;
當我看向square.txt日誌
,看起來像正方形一些奇怪的字符,當我嘗試去SDL_Rect臨時的數據使用,應用剛剛結束,我在做什麼這裏弄錯了值?
請閱讀http://sscce.org,瞭解如何以及爲什麼您應該將代碼減少到一個簡單的測試用例。 – 2012-03-20 17:22:35