0

私は宿題をしていて、クラス図に従おうとしています。Book.cppファイルでは、コメントされたテキストがどこであっても、次のようなエラーが発生しますUnhandled exception at 0x00CE3F1B in Lab 1.exe: 0xC0000005: Access violation reading location 0xCCCCCD1C

次のコードでは、ポインターを使用して何をすべきか、このオブジェクト変数をこのコンストラクターに渡す方法、またはポインターを使用して何をすべきかがわかりません。私は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;
}

私はとても迷っているので、クラス図。私は構造を正しく行ったと信じており、変数を渡すことのいくつかは良いことです。しかし、私はポインタを使ってそれを行う方法がわかりません。

ここに画像の説明を入力してください

4

1 に答える 1

1

クラスオブジェクトには、初期化されていないパブリッシャーへのポインターと作成者へのポインターの2つのポインターがあるようです。これらのオブジェクトでメンバー関数を呼び出そうとすると、未定義の動作が発生し、特にアプリケーションがクラッシュします。

于 2012-09-04T00:48:17.820 に答える