2011-02-14 163 views
1

我有上述問題,當我試圖設置一個字符串(存儲在一個類)等於另一個字符串。我梳理和梳理,試圖找出我是否沒有初始化任何變量,但我找不到這樣的情況。在調試mod中,我得到了上述錯誤。在發佈模式下,它會掛起,Win7會查找問題,不會重大放棄或重試窗口。這裏是相關的代碼,如果你覺得它應該包括在內,那麼在我的主程序中有另一個頭文件,我將包含導致錯誤的行。語言顯然是C++。0xC0000005:訪問衝突讀取位置0xccccccd0。 C++

//Error occurs in this area: 
    Car one; 
    one = two; 
    one.addExtra ("Windows"); 

    log << "Car one: " << one << endl; 
    two = Car(one); // call copy constructor. 
//I realize when I call the first one = two, there are no extras 
//stored int Car one, which is what differs between the two. Remaining 
//code. Extras header: 

#include <iostream> 
#include <string> 
#include <string.h> 
using namespace std; 

class Extras 
{ 
public: 
    friend class Car; 
    friend int main(); 
    friend ostream& operator << (ostream& os, const Extras& in); 
    friend class CarLot; 
    Extras(const Extras& other); 
    Extras& operator=(Extras &rhs); 
    Extras(string in); 
    Extras(); 
    ~Extras(); 
    void modify_ext(string* in); 
    //string ex_list; 
private: 
    int place; 
    string *ex_list; 
}; 
//Extras.cpp: 
#include "Extras.h" 

Extras::Extras(string in) 
{ 
    delete ex_list; 
    ex_list = new string; 
    place = 0; 
    //ex_list = new string[4]; 
    (*ex_list) = in; 
    place++; 
} 

Extras::Extras() 
{ 
    //ex_list = new string[4]; 
    place = 0; 
    //for(int i = 0; i < 4; i++) 
    ex_list = new string; 
    *ex_list = "0"; 
} 

//Overloaded << operator for Extras class to 
//easily output array contents 
ostream& operator<< (ostream& os, Extras const &in) 
{ 
    os << *(in.ex_list); 
    return os; 
} 

Extras& Extras::operator=(Extras &rhs) 
{ 
    if(this != &rhs) 
    { 
     //string temp; 
     //temp = rhs.ex_list; 
     modify_ext(rhs.ex_list); 
     cout << endl << endl << ex_list << endl << endl; 
     place = rhs.place; 
    } 
    return *this; 
} 

Extras::Extras(const Extras& other) : place(other.place), ex_list(other.ex_list) 
{ 
    //for(int i = 0; i < 4; i++) 
     //ex_list = other.ex_list; 
} 

void Extras::modify_ext(string* in) 
{ 
    delete ex_list; 
    ex_list = new string; 
    (*ex_list).resize((*in).size()); 
    for(unsigned int i = 0; i < (*in).size(); i++) 
     ex_list[i] = in[i]; 
} 

Extras::~Extras() 
{ 
    delete ex_list; 
    place = 0; 
} 

//Car Header: 
#include "Extras.h" 

class Car 
{ 
public: 
    friend class Extras; 
    friend Extras& Extras::operator=(Extras &rhs); 
    friend int main(); 
    friend ostream& operator<< (ostream& os, const Car& in); 
    friend class CarLot; 
    friend void add_extra(); 
    ~Car(); 
    Car(); 
    Car(Car& other); 
    Car(string in_name, int in_year, string in_color, float in_cost); 
    Car& operator=(Car const &rhs); 
    void edit_extr(int in); 
    void addExtra(string in); 
private: 
    string name, color; 
    int year, extr_num; 
    float cost; 
    Extras *extr; 
}; 

//Car.cpp: 


#include "car.h" 

//Constructor 
Car::Car(string in_name, int in_year, string in_color, float in_cost) 
{ 
    name = in_name; 
    color = in_color; 
    year = in_year; 
    cost = in_cost; 
    extr = new Extras[3]; 
    extr_num = 0; 
} 

//Overloaded = operator 
Car& Car::operator=(Car const &rhs) 
{ 
    if(this != &rhs) 
    { 
     name = rhs.name; 
     color = rhs.color; 
     year = rhs.year; 
     cost = rhs.cost; 
     //delete extr; 
     extr = rhs.extr; 
     extr_num = rhs.extr_num; 
    } 
    return *this; 

} 



//Default Constructor 
Car::Car() 
{ 
    name = "TEMP"; 
    color = "BLUE"; 
    year = 0; 
    cost = 0; 
    extr = new Extras[3]; 
    extr_num = 0; 
} 

//Destructor 
Car::~Car() 
{ 
    delete extr; 
    extr = NULL; 
} 

//Copy constructor 
Car::Car(Car& other) : name(other.name), color(other.color), year(other.year), 
    cost(other.cost), extr_num(other.extr_num) 

{ 
    //delete extr; 
    for(int i = 0; i < extr_num; i++) 
    { 
     extr[i].modify_ext(other.extr[i].ex_list); 
     extr[i].place = other.extr[i].place; 
    } 
} 





//Overloaded << operator for Car class 
ostream& operator<< (ostream& os, const Car& in) 
{ 
    os.precision(2); 
    os << in.name << ", " << in.year << ", " 
     << in.color << ", $"<< in.cost << ", "; 
    os << "extras include: "; 
    for(int k = 0; k < in.extr_num; k++) 
    { 
     os << in.extr[k] << ", "; 
    } 
    os << endl; 
    return os; 
} 

void Car::edit_extr(int in) 
{ 
    Extras* temp; 
    temp = new Extras[in]; 
    for(int i = 0; i < in; i++) 
     temp[i] = extr[i]; 
    extr_num = in; 
    delete extr; 
    extr = temp; 
} 

void Car::addExtra(string in) 
{ 
    if(extr_num == 3) 
    { 
     //log << "Car has too many extras."; 
     return; 
    } 
    //edit_extr(extr_num + 1); 
    *(extr[extr_num].ex_list) = in; 
    extr[extr_num].place++; 
    extr_num++; 
} 

正如我所說的,我多了一個額外的頭,另一個類,並且主程序如果這些需要被包括在內,但我想這是綽綽有餘碼以上(對不起!)的人通過看。任何幫助將不勝感激。

+4

您應該總是嘗試*最小化*重現問題所需的代碼。至少嘗試在調試器下運行它。 – 2011-02-14 19:20:26

+1

我不認爲這個問題值得任何答案... – 2011-02-14 19:54:12

+0

Piotr,我做了這兩個。我在我的OP中說過,我在調試中運行它,並且我是如何得到錯誤是問題的標題,並且我試圖單獨測試每個函數並且沒有得到結果。之後我開始重新添加東西。抱歉。 – zmarine 2011-02-14 21:16:34

回答

8

我所看到的那個地方不對頭:


two = Car(one); // call copy constructor. 

不,它創建了拷貝構造一個臨時對象,通過這operator=()two,然後破壞了暫時的。


Extras& operator=(Extras &rhs); 

應該是:

Extras& operator=(const Extras &rhs); 

Extras::Extras(string in) 
{ 
    delete ex_list; 
    ex_list = new string; 
    place = 0; 
    //ex_list = new string[4]; 
    (*ex_list) = in; 
    place++; 
} 

更好:

Extras::Extras(const string& in): place(1), ex_list(new string(in)) 
{ 
} 

Extras::Extras(const Extras& other) : place(other.place), ex_list(other.ex_list) 
{ 
    //for(int i = 0; i < 4; i++) 
     //ex_list = other.ex_list; 
} 

看看您的默認構造函數,顯然Extras對象擁有ex_list中的字符串。但是,此複製構造函數聲明原始對象的所有權爲ex_list。它應使自己的副本:

Extras::Extras(const Extras& other): place(other.place), 
    ex_list(new string(other.ex_list)) 
{ 
} 

void Extras::modify_ext(string* in) 
{ 
    delete ex_list; 
    ex_list = new string; 
    (*ex_list).resize((*in).size()); 
    for(unsigned int i = 0; i < (*in).size(); i++) 
     ex_list[i] = in[i]; 
} 

你複製的字符串。所有你需要的是:

void Extras::modify_ext(const string* in) 
{ 
    *ex_list = *in; 
} 

移動到Car ...

friend class Extras; 
friend Extras& Extras::operator=(Extras &rhs); 
friend int main(); 
friend ostream& operator<< (ostream& os, const Car& in); 
friend class CarLot; 
friend void add_extra(); 

你應該考慮重構代碼,以擺脫這些。


Car(Car& other); 
Car(string in_name, int in_year, string in_color, float in_cost); 

應該是:傳遞對象的功能時

Car(const Car& other); 
Car(const string& in_name, int in_year, const string& in_color, float in_cost); 

引用是你的朋友。


我打算在這裏停下來。

5

在您的構造函數中,您正在刪除ex_list。它還沒有分配,所以這是錯誤的。刪除它,並在分配新字符串時執行此操作:

ex_list = new string(in); 

這樣你就可以使用字符串拷貝構造函數。你可以在構造函數中刪除你正在嘗試做的其他任何事情,因爲這會爲你做。

編輯:

其實,還有所有在本代碼一噸的問題。有沒有什麼理由讓你的字符串在內部成爲一個指針?您在一堆不同的地方沒有正確使用指針。當我向下滾動時,我只注意到第一個。

2

調試版本中的分配器使用VC++使用一些魔術值來填充分配的區域。特別是,0xCC是已分配的內存,但現在已釋放。因此,地址0xCCCCCCD0看起來像一個小的偏移量(例如,一個結構或類成員)從內存中的指針釋放。鑑於此,Mark Loeser的回答看起來很有希望。

相關問題