2015-12-20 221 views
0

我試圖從我創建的類使用std :: copy C++函數複製的對象的列表,但函數崩潰接下來的錯誤消息:0000005:訪問衝突讀取位置 0xFFFFFFFFFFFFFFFF在 MyProject.exe在0x00007FFA91BAC570(msvcr120.dll)C + +:std ::複製失敗,訪問衝突讀取位置錯誤

未處理的異常。

這個類非常簡單,我不知道什麼會導致複製函數崩潰這樣的錯誤。 類看起來如下:

class XMLObjects 
    { 
    private: 
     int id; 
     string name; 
     int leftUpCornerX; 
     int leftUpCornerY; 
     int rightDownCornerX; 
     int rightDownCornerY; 
     bool isFound; //it's true if this object was found in the OR file and in the GT file 

public: 
    XMLObjects(){} 
    XMLObjects(int _id){ id = _id; } 
    XMLObjects(int _id, string _name, int _leftUpCornerX, int _leftUpCornerY, int _rightDownCornerX, int _rightDownCornerY) { id = _id; name = _name; leftUpCornerX = _leftUpCornerX; leftUpCornerY = _leftUpCornerY; rightDownCornerX = _rightDownCornerX; rightDownCornerY = _rightDownCornerY; isFound = false; } 
    ~XMLObjects(){} 

    void setID(int _id){ id = _id; } 
    void setName(string _name){ name=_name; } 
    void setLeftUpCorner(int _leftUpCornerX, int _leftUpCornerY){ leftUpCornerX = _leftUpCornerX; leftUpCornerY = _leftUpCornerY; } 
    void setRightDownCorner(int _rightDownCornerX, int _rightDownCornerY){ rightDownCornerX = _rightDownCornerX; rightDownCornerY = _rightDownCornerY; } 
    void objectWasFound(){ isFound = true; } 

    int getID(){ return id; } 
    string getName(){ return name; } 
    int getLeftUpCornerX(){ return leftUpCornerX; } 
    int getLeftUpCornerY(){ return leftUpCornerY; } 
    int getRightDownCornerX(){ return rightDownCornerX; } 
    int getRightDownCornerY(){ return rightDownCornerY; } 
    bool isObjectFound(){ return isFound; } 
}; 

複印功能的調用看起來如下:

list<XMLObjects> objects; 
objects.push_front(obj1); 
objects.push_front(obj2); 
objects.push_front(obj3); 
list<XMLObjects> NewObjectsList; 
std::copy(objects.begin(), objects.end(), NewObjectsList.begin()); 

誰能幫助?

+5

你必須使用'inserter' – Jarod42

+4

爲了擴大是,'NewObjectsList'是空的,而' std :: copy'不會調整它的大小。 – hlt

+2

爲什麼不'名單 NewObjectsList = objects'? – LogicStuff

回答

4

std::copy不調整輸出,讓你擁有前調整您的容器或使用特殊的迭代其中插入元件。

您可以使用下面的代碼。

std::copy(objects.begin(), objects.end(), 
      std::inserter(NewObjectsList, NewObjectsList.end())); 

正如評論所指出的,在你的情況,你可以直接做一個副本:

list<XMLObjects> NewObjectsList = objects; 
+1

或簡單地'std :: back_inserter(NewObjectsList)' –