让我解释一下情况:
我有一个类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 temp的de数据时,应用程序只是终止,我在这里做错了什么来获取值?
答案 0 :(得分:-1)
你可能正在输出字符。将这些输出到ostream时,您将获得ASCII字符,而不是ASCII字符的数字值。试试这个:
log << "x = " << (int) temp.x
<< "y = " << (int) temp.y;
'char'经常用作1字节整数的简写。它们适用于此,除了当它们将它们输出到流时,它会尝试将它们输出为ASCII字符,而不是作为一个字节的整数。将字符转换为真正的整数通常可以解决问题。