Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;标准向量推回不';好像不行_C++_Vector_Sdl_Libconfig - Fatal编程技术网

C++ C++;标准向量推回不';好像不行

C++ C++;标准向量推回不';好像不行,c++,vector,sdl,libconfig,C++,Vector,Sdl,Libconfig,我正在用SDL制作一个游戏,它使用libconfig从文件中读取一些设置。问题是,我创建了一个名为ClipList的类,其中包含一个std::vector来存储设置,但是当尝试将SDL\Rect对象添加到向量时,由于某些原因,push\u back不起任何作用,结果是一个空向量 这是一节课: class ClipList { public: ClipList(); ClipList(int); virtual ~ClipList();

我正在用SDL制作一个游戏,它使用libconfig从文件中读取一些设置。问题是,我创建了一个名为
ClipList
的类,其中包含一个
std::vector
来存储设置,但是当尝试将
SDL\Rect
对象添加到向量时,由于某些原因,push\u 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
{
公众:
ClipList();
ClipList(int);
virtual~ClipList();
void addClip(int,int,int,int);
void getClip(int,SDL_Rect*);
int getLength();
受保护的:
私人:
向量剪辑;
};
ClipList::ClipList(intl)
{
3.保留(l);
}
void ClipList::addClip(int x,int y,int w,int h){
SDL_Rect;
rect.x=x;
矩形y=y;
矩形w=w;
矩形h=h;
夹子。推回(rect);
}
void ClipList::getClip(inti,SDL_Rect*Rect){
rect=&(clips.at(i));
}
int ClipList::getLength(){
返回剪辑。大小();
}
这是我初始化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;
    }
}
void set_片段(配置*placlips、ClipList*片段、ClipList*flipclips){
常量设置&root=placlips->getRoot();
int x,y,w,h;
试一试{
设置&clipsett=root[“clips”];
int-cliplen=clipsett.getLength();
clips=新的ClipList(cliplen);
flipclips=新的ClipList(cliplen);
对于(inti=0;iaddClip(x,y,w,h);
}
}捕获(常量设置NotFoundException和nfex){

cerr我将猜测函数的签名

void set_clips(Config* placlips,ClipList* clips, ClipList* flipclips);
是罪魁祸首。您正在为此函数中的
clips
flipclips
分配内存,但由于指针是按值传递的,因此调用函数看不到分配的内存

如果将函数签名更改为:

void set_clips(Config* placlips, ClipList*& clips, ClipList*& flipclips);
您的问题应该消失。

剪辑。push_back(rect)
工作正常。您的
set_clips
函数分配新的ClipList实例,但不会将这些指针传回调用方。调用方可能正试图将垃圾指针用作初始化实例,这就是您遇到segfault的原因

您需要将创建的对象传回。您应该使用类似std::shared\u ptr的方法来完成这项工作,而不是使用空指针

更新如何在不使用std::shared_ptr的情况下执行此操作:

您需要跟踪所有权并处理异常。就实际传递而言,我使用的规则(最初是从“大规模C++软件设计中的LakOS”)是返回值的参数(正如您正试图使用的)。是指针,只读参数是按值或常量引用。返回值排在第一位

因此,您的
set_clips
函数应该如下所示:

void set_clips(ClipList** clips, ClipList** flip_clips, Config const& placlips)
ClipList* clips = 0;
ClipList* flip_clips = 0;
set_clips(&clips, &flip_flips, placlips);
// ... then do whatever comes next.
调用
set\u clips
时,向将接收分配值的每个指针传递一个指针,并向未被函数修改的placlips对象传递一个常量引用

你可以这样说:

void set_clips(ClipList** clips, ClipList** flip_clips, Config const& placlips)
ClipList* clips = 0;
ClipList* flip_clips = 0;
set_clips(&clips, &flip_flips, placlips);
// ... then do whatever comes next.

但是将这些规则与std::shared_ptr或boost::shared_ptr相结合会更好,而且“现代C++”样式。

如果没有共享的ptr,我怎么做呢?是的,这就是问题所在,虽然通过引用传递指针得到了编译
SDL\Rect
对象的代码,但仍然没有存储在内存中。我通过在
main
中初始化ClipList对象,并通过引用传递ClipList对象而不是指针。