我正在使用 SDL 制作一个游戏,该游戏使用 libconfig 从文件中读取一些设置。问题是我创建了一个ClipList
包含 astd::vector<SDL_Rect>
来存储设置的类,但是当尝试将SDL_Rect
对象添加到向量时,由于某种原因 push_back 什么也没做,我最终得到一个空向量。
这是课程:
class ClipList
{
public:
ClipList();
ClipList(int);
virtual ~ClipList();
void addClip(int,int,int,int);
void getClip(int,SDL_Rect*);
int getLength();
protected:
private:
std::vector<SDL_Rect> clips;
};
ClipList::ClipList(int l)
{
clips.reserve(l);
}
void ClipList::addClip(int x,int y,int w,int h){
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
clips.push_back(rect);
}
void ClipList::getClip(int i,SDL_Rect* rect){
rect = &(clips.at(i));
}
int ClipList::getLength(){
return clips.size();
}
这是我初始化 ClipList 对象的函数。这个函数是从 main 调用的。
void set_clips(Config* placlips,ClipList* clips, ClipList* flipclips){
const Setting& root = placlips->getRoot();
int x,y,w,h;
try{
Setting& clipsett = root["clips"];
int cliplen = clipsett.getLength();
clips = new ClipList(cliplen);
flipclips = new ClipList(cliplen);
for(int i=0;i<cliplen;i++){
const Setting& c = clipsett[i];
if(!(c.lookupValue("x",x)&&c.lookupValue("y",y)&&c.lookupValue("w",w)&&c.lookupValue("h",h))){
continue;
}
clips->addClip(x,y,w,h);
}
}catch(const SettingNotFoundException &nfex){
cerr << "Setting not found at" << nfex.getPath() << endl;
}
}
无论对象是否在或中ClipList
初始化,都不起作用。向量的容量发生了变化,但没有对象被存储,所以如果我尝试对向量执行任何其他操作,即使检查向量是否为空,我最终也会出现段错误。main
set_clips
clips.push_back(rect)