2014-03-02 17 views
2

,當我嘗試設置C++控制檯Application1.exe引發了斷點

cub.SetArray(cube); 

我得到一個錯誤

Console Application1.exe has triggered a breakpoint 

我做錯了什麼?當我嘗試調試cub -> cubesarray時,我得到大小-842150451。我不明白why.Here的我所有的代碼

class Cube{ 
public: 
    static const int Change_ARRAY = 5; 

private: 
    string color; 
    int size; 
    int *walls; 
    int n; // current size of array 
    int maximumsize; // maximum size of array 
    void Increase(int many); 
public: 
    Cube(int maximumsize = 0); 
    ~Cube(); 
    void SetWalls(int wall); 
    void SetColor(string color); 
    void SetSize(int size); 

    string GetColor(){return color;} 
    int GetWalls(int i){return walls[i];} 
    int GetSize(){return size;} 

    int GetN(){return n;} 
}; 

Cube::Cube(int maximumsize):n(0), maximumsize(maximumsize), size(size), walls(NULL){ 
    if(maximumsize > 0){ 
     walls = new int[maximumsize]; 
    } 
} 

Cube::~Cube(){ 
    if(walls){ 
     delete [] walls; 
    } 
} 

void Cube::Increase(int many){ 
    if(many > maximumsize){ 
     int *newest = new int[many]; 
     for(int i=0; i<n; i++) 
      newest[i] = walls[i]; 
     delete [] walls; 
     walls = newest; 
     maximumsize = many; 
    }else if(many < maximumsize){ 
     int *newest = new int[many]; 
     for(int i=0; i<many; i++) 
      newest[i] = walls[i]; 
     delete [] walls; 
     walls = newest; 
     n = maximumsize = many; 
    } 
} 

void Cube::SetWalls(int wall){ 
    if(n == maximumsize) Increase(n + Change_ARRAY); 
    walls[n] = wall; 
    n++; 
} 

void Cube::SetColor(string color){ 
    this->color = color; 
} 

void Cube::SetSize(int size){ 
    this->size = size; 
} 

class CubesArray{ 
public: 
    static const int Change_Array = 5; 
private: 
    Cube *cubesarray; 
    int currentsize; // current size of array 
    int maxsize; // maximumsize 
    void Change (int kk); 
public: 
    CubesArray(int maxsize = 1); 
    ~CubesArray(); 

    void SetArray(Cube c); 
    Cube GetArray(int ind){return cubesarray[ind];} 
    int GetCsize(){return currentsize;} 
}; 

CubesArray::CubesArray(int maxsize):cubesarray(NULL), currentsize(0), maxsize(maxsize){ 
    if(maxsize > 0){ 
     cubesarray = new Cube[maxsize]; 
    } 
} 

CubesArray::~CubesArray(){ 
    if(cubesarray){ 
     delete [] cubesarray; 
    } 
} 

void CubesArray::Change(int kk){ 
    if(kk > maxsize){ 
     Cube *newarr = new Cube[kk]; 
     for(int i=0; i<currentsize; i++) 
      newarr[i] = cubesarray[i]; 
     delete [] cubesarray; 
     cubesarray = newarr; 
     maxsize = kk; 
    }if(kk < maxsize){ 
     Cube *newarr = new Cube[kk]; 
     for(int i=0; i<kk; i++) 
      newarr[i] = cubesarray[i]; 
     delete [] cubesarray; 
     cubesarray = newarr; 
     currentsize = maxsize = kk; 
    } 
} 

void CubesArray::SetArray(Cube cub){ 
    if(currentsize = maxsize) Change(currentsize + Change_Array); 
    cubesarray[currentsize] = cub; 
    currentsize++; 
} 

void Read(CubesArray & cub); 

int main(){ 
    CubesArray cub; 

    Read(cub); 

    system("pause"); 
    return 0; 
} 

void Read(CubesArray & cub){ 
    string color; 
    int size; 
    int i=0; 
    Cube cube; 
    ifstream fd(Data); 
    while(!fd.eof()){ 
     fd >> color >> size; 
     cube.SetSize(size); 
     cube.SetColor(color); 
     cout << cube.GetColor() << " " << cube.GetSize() << " "; 
     while(fd.peek() != '\n' && !fd.eof()){ 
      int w; 
      fd >> w; 
      cube.SetWalls(w); 
      cout << cube.GetWalls(i) << " "; 
      cub.SetArray(cube); // when I set cube to cub I get this error!!! 
      i++; 
     } 
     cout << endl; 
     fd.ignore(); 
    } 
} 
+0

只是,'std :: vector 牆壁;''立方體'和'std :: vector '會使*這個代碼的一個*顯着量消失。你的初始化列表中的'size(size)'是錯誤的。我在想'size(0)'? – WhozCraig

+0

我知道,但我需要找出動態數組有什麼問題 – user3369351

+0

從我提到的size()問題開始,然後按照您的方式找到下面的答案(其中可能已包含該答案)。 – WhozCraig

回答

4

變化:

if(currentsize = maxsize) 

要:

if(currentsize == maxsize) 

另外,這裏是你真正的問題:

您在class Cube中沒有複製構造函數,所以walls只要按值發送Cube實例,例如cub.SetArray(cube),就不會正確複製陣列。

如下您必須定義它:

Cube::Cube(const Cube& cube):n(cube.n),maximumsize(cube.maximumsize),size(cube.size),wall(NULL) 
{ 
    if (maximumsize > 0) 
    { 
     walls = new int[maximumsize]; 
     for (int i=0; i<maximumsize; i++) 
      wall[i] = cube.wall[i]; 
    } 
} 

而且你有沒有賦值運算符class Cube,所以walls陣列每當你分配一個Cube實例到另一個沒有被正確拷貝,例如,cubesarray[currentsize] = cub

如下您必須定義它:

Cube& Cube::operator=(const Cube& cube) 
{ 
    n = cube.n; 
    maximumsize = cube.maximumsize; 
    size = cube.size; 
    wall = NULL; 
    if (maximumsize > 0) 
    { 
     walls = new int[maximumsize]; 
     for (int i=0; i<maximumsize; i++) 
      wall[i] = cube.wall[i]; 
    } 
    return *this; 
} 

順便說一句,在拷貝構造函數,你可以簡單地調用賦值運算符(消除編碼冗餘):

Cube::Cube(const Cube& cube) 
{ 
    if (this != &cube) 
     *this = cube; 
} 
+0

謝謝,但我仍然得到控制檯Application1.exe觸發了一個斷點 – user3369351

+0

@ user3369351:答案更新。 –

+0

@barakmanos你想要完成*相反*完成。即使用按值賦值運算符參數並利用[複製/交換語句](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)出於多種原因,其中包括例外保護。 – WhozCraig

1

你的魔方類違反了三條規則。看這裏:

void CubesArray::SetArray(Cube cub){ // calls copy constructor 

該調用會創建一個Cube類的副本。您的Cube課程不可安全複製。請看到這一點,向下滾動到管理資源部分:What is The Rule of Three?

你應該通過立方體引用或常量引用,而不是價值。這樣做可能會糾正你現在有的錯誤,但仍然是,你的班級有問題。

相關問題