2012-09-04 23 views
0

我做了功課,我試圖按照類圖一些解釋。在Book.cpp文件中,凡是評論文字它都會導致錯誤說:Unhandled exception at 0x00CE3F1B in Lab 1.exe: 0xC0000005: Access violation reading location 0xCCCCCD1C我的指針違反訪問限制errror,我想對我應該怎麼做書非默認構造函數

在下面的代碼,我不知道我應該用指針來這樣做,我不知道我應該怎麼通過這個對象的變量到這個構造函數或我應該用它做的事情。我很困惑與c + +。我在這篇文章的末尾附上了類圖。

Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price) 
{ 
    setTitle(title); 
    setPrice(price); 
} 

Book.cpp文件:

#include <iostream> 
#include <sstream> 
using namespace std; 

#include "Book.h" 

Book::Book() 
{ 
} 

Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price) 
{ 
    setTitle(title); 
    setPrice(price); 
} 

void Book::setTitle(string title) 
{ 
    this->title = title; 
} 

void Book::setAuthorName(string first, string last) 
{ 
    //pAuthor->setFirstName(first); 
    //pAuthor->setLastName(last); 
} 

void Book::setPublisher(string name, string address, string city) 
{ 
    //pPublisher->setName(name); 
    //pPublisher->setAddress(address); 
    //pPublisher->setCity(city); 
} 

void Book::setPrice(double price) 
{ 
    this->price = price; 
} 

string Book::convertDoubleToString(double number) 
{ 
    return static_cast<ostringstream*>(&(ostringstream() << number)) -> str(); 
} 

string Book::getBookInfo() 
{ 
    return "0"; //title + "\n" + pAuthor->getFullName() + "\n" + pPublisher->getPublisherInfo() + "\n" + "$" + convertDoubleToString(price); 
} 

Book.h文件

#include "Publisher.h" 
#include "Author.h" 

class Book 
{ 
    public: 
     Book(); 
     Book(string title, Author *pAuthor, Publisher *pPublisher, double price); 
     ~Book(); 
     void setTitle(string title); 
     void setAuthorName(string first, string last); 
     void setPublisher(string name, string address, string city); 
     void setPrice(double price); 
     string convertDoubleToString(double number); 
     string getBookInfo(); 

    private: 
     string title; 
     double price; 
     Author *pAuthor; 
     Publisher *pPublisher; 
}; 

這是我在main.cpp中

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

#include "Book.h" 

int main() 
{ 
    system("cls"); 

    cout << "Book 1" << endl; 

    Author *pAuthor = new Author("John", "Doe"); 
    Publisher *pPublisher = new Publisher("Wrox", "10475 Crosspoint Blvd.", "Indianapolis"); 
    Book *pBook = new Book("Memory Management", pAuthor, pPublisher, 39.99); 

    cout << pBook->getBookInfo() << endl; 

    cout << endl << "Book 2" << endl; 

    Book book; 

    book.setTitle("Advanced C++ Programming"); 
    book.setAuthorName("Linda", "Smith"); 
    book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond"); 
    book.setPrice(49.99); 

    cout << book.getBookInfo() << endl << endl; 

    system("pause"); 

    return 0; 
}; 

Author.cpp

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

#include "Author.h" 

Author::Author() 
{ 
} 

Author::Author(string first, string last) 
{ 
    setFirstName(first); 
    setLastName(last); 
} 

string Author::getFullName() 
{ 
    return firstName + " " + lastName; 
} 

void Author::setFirstName(string first) 
{ 
    this->firstName = first; 
} 

void Author::setLastName(string last) 
{ 
    this->lastName = last; 
} 

Publisher.cpp

#include <iostream> 
#include <sstream> 
#include <string> 
using namespace std; 

#include "Publisher.h" 

Publisher::Publisher() 
{ 
} 

Publisher::Publisher(string name, string address, string city) 
{ 
    setName(name); 
    setAddress(address); 
    setCity(city); 
} 

string Publisher::getPublisherInfo() 
{ 
    return string(name + "\n" + address + "\n" + city); 
} 

void Publisher::setName(string name) 
{ 
    this->name = name; 
} 

void Publisher::setAddress(string address) 
{ 
    this->address = address; 
} 

void Publisher::setCity(string city) 
{ 
    this->city = city; 
} 

類圖,因爲我迷茫。我相信我做了正確的結構,一些傳遞變量是好的。但我只是不知道如何用指針來做到這一點。

enter image description here

+0

您應該提供類的定義。另外,你爲什麼通過指針存儲作者/發佈者?您的意思是分享共享該屬性的所有書籍的作者/發佈者對象嗎? –

+0

您使用的語法本身提示Java背景?如果是這樣,C/C++指針可能有點令人生畏。我將開始確保所有對象指針都已正確初始化。 – WhozCraig

+0

我已經添加了我必須遵循的類圖。 – HelpNeeder

回答

1

它看起來像你的類的對象有兩個指針,一個出版商,另一個被從未初始化的作者。當您嘗試調用這些對象的成員函數時,會導致未定義的行爲,特別是應用程序崩潰。

+0

我在Book.h的Book.cpp的頭文件中初始化了指針pPublisher和pAuthor。 – HelpNeeder

+0

@HelpNeeder:你應該發佈代碼,但我非常懷疑它。那些看起來像成員變量,並且它們在構造函數中沒有提及,除非它們是全局變量......你看到了嗎?如果不提供真實的代碼,則無法回答。幫助別人幫助你:提供完整的代碼! –

+0

@HelpNeeder:你認爲你在哪裏初始化了指針?我正在查看代碼並沒有看到它。你已經聲明瞭變量,但是沒有在Book.h中的代碼中提供初始化。 –

相關問題