2015-09-15 50 views
-2

嗨,我很新,所以很抱歉,如果我做了任何正式的錯誤。調試斷言失敗 - 表達式:_CrtlsValidHeapPointer(塊)

我有問題與我的圖書館計劃有兩個類:書和人。 我寫了_customer = customer後出現錯誤;在書的結構。

我該如何解釋錯誤?我究竟做錯了什麼?

Person類

class Person{ 

private: 
    char* _name; 
    int _id; 
    static int personCounter; 

public: 
    void setName(char* name); 
    void setId(int id); 
    char* getName(); 
    char* getName() const; 
    int getId(); 
    int getId()const; 
    void showData(); 
    Person(); 
    Person(int id, char* name); 
    Person(const Person &tempPerson); 
    ~Person(); 

}; 

人的拷貝構造函數

Person::Person(const Person &tempPerson) { 
    _id = tempPerson.getId(); 
    _name = new char[strlen(tempPerson.getName())]; 
    strcpy(_name, tempPerson.getName()); 
    personCounter--; 
} 

Book類

class Book{ 

private: 
    char* _title; 
    char* _author; 
    char* _genre; 
    bool _borrowed; 
    Person _customer; 
    static int counter; 

public: 
    void setTitle(char* title); 
    void setAuthor(char* author); 
    void setGenre(char* genre); 
    void setStatus(bool borrowed); 
    void setPerson(Person customer); 

    char* getTitle(); 
    char* getAuthor(); 
    char* getGenre(); 
    bool getStatus(); 
    Person getPerson(); 
    void showData(); 

    Book(); 
    Book(char* title, char* author, char* genre, bool borrowed, const Person &customer); 

    ~Book(); 

}; 

圖書構造

Book::Book(char* title, char* author, char* genre, bool borrowed,const Person &customer){ 
    counter++; 



    _title = new char[strlen(title) + 1]; 
    strcpy(_title,title); 

    _author = new char[strlen(author) + 1]; 
    strcpy(_author, author); 

    _genre = new char[strlen(genre) + 1]; 
    strcpy(_genre, genre); 

    _borrowed = borrowed; 

    _customer = customer; 

} 
+3

這不是編譯時間,而是運行時錯誤。斷言用於指示某些調用該功能所需的條件未被滿足。使用你的調試器來檢查你實際使用哪個參數來調用它。 –

+2

您在Person拷貝構造函數中忘記了strlen上的+1。這就是爲什麼人們使用C++而不是原始字符指針的字符串類。 – antlersoft

+0

另外,我會緊急建議您不要自己管理動態內存分配。改爲使用標準容器類或[智能指針](http://en.cppreference.com/w/cpp/memory)。 –

回答

0

解決了它。 「=」運算符沒有覆蓋功能。