2012-03-20 52 views
-1

让我解释一下这种情况:无法获得的另一种载体内从矢量数据

我有一个类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临时的数据使用,应用刚刚结束,我在做什么这里弄错了值?

+3

请阅读http://sscce.org,了解如何以及为什么您应该将代码减少到一个简单的测试用例。 – 2012-03-20 17:22:35

回答

-1

您可能正在输出字符。将这些输出到ostream时,您将获得ASCII字符,而不是ASCII字符的数字值。试试这个:

log << "x = " << (int) temp.x 
    << "y = " << (int) temp.y; 

'char'经常用作1字节整数的简写。它们适用于此目的,除了将它们输出到流时,它会尝试将它们输出为ASCII字符,而不是一个字节的整数。将角色转换为真正的整数通常可以解决问题。

+0

仍然在文件上保存相同的奇怪字符,这可能是一些错误的内存访问? – 2012-03-20 17:26:32

+2

SDL_Rect组件不是char类型。它们是Sint16,它在大多数系统上只是一个“short”的typedef。 – 2012-03-20 17:27:43

+0

我在方法上使用相同的“模式”保存数据,只是在那里我从矢量帧中获取数据的温度并不节约,并且当我尝试使用temp来终止同一瞬间时终止做一点事。 – 2012-03-20 17:28:59