c++ - 我的指针违反了访问限制错误,我想解释一下我应该在 Book 非默认构造函数中做什么

标签 c++ oop pointers

<分区>

我正在做作业,我正在尝试遵循类图。在 Book.cpp 文件中,凡是 commented text 都会导致错误提示: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;
};

作者.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;
}

发布者.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

最佳答案

看起来您的类对象有两个指针,一个指向发布者,另一个指向从未初始化的作者。当您尝试调用这些对象的成员函数时,您会导致未定义的行为,尤其是您的应用程序崩溃。

关于c++ - 我的指针违反了访问限制错误,我想解释一下我应该在 Book 非默认构造函数中做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255466/

相关文章:

c++ - 如何区分 C++ 中的隐式/显式构造函数调用?

C++,对象数组 VS 指向这些对象的指针数组

c++ - 返回指向 const 数据的 const 双指针

c++ - 在执行 DFS 时在 Boost::graph 中维护迭代器

c++ - 在二维 vector 中找到一个元素,然后将其删除

c++ - 我的代码能否使用 'T' 或 'const T &' 特化,以可用者为准?

Java : Using parent class method to access child class variable

java - 有什么方法可以使 A 类的变量只能被特定接口(interface)的类修改吗?

python - python强制创建新的内存对象

c++ - 具有继承性的未初始化指针对象