似乎我没有正确传递对象的指针,但为什么呢?
It seems like I'm not passing pointers of objects correctly, but why?
我正在使用对象的指针,并能够使用它们的内存地址来传递和检索信息。
我的程序只传递了书名,没有其他信息,我不知道为什么。
如何使该程序正确存储和检索信息?我正在使用指针来传递值,我应该更改主函数以外的其他内容吗?
这是我的主.cpp文件:
#include <iostream>
#include <string>
using namespace std;
#include "Book.h"
int main()
{
system("cls");
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;
system("pause");
return 0;
};
book.cpp文件:
#include <iostream>
#include <sstream>
using namespace std;
#include "Book.h"
Book::Book()
{
}
Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
this->title = title;
this->price = price;
}
Book::~Book()
{
}
您将pAuther和pPublisher传递给Book构造函数,但不对它们执行任何操作。
Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
this->title = title;
this->price = price;
}
在这里,你传递指针,但不使用它们。
您的book()构造函数需要使用pAuthor和pPublisher中的数据调用setAuthorName和setPublisher函数。
相关文章:
- 为什么指针对象没有调用默认构造函数
- 如何访问双指针对象中的方法
- 如何使用智能指针对象执行成员函数指针
- 调用带有指针对象错误的指针变量
- 如何从模板类重载创建的指针对象上的运算符?
- C++指针对象的运算符重载
- 如何初始化数组指针对象
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- C++指针对象差异
- 涉及继承,指针对象的交易以及何时进行的问题
- 表达式必须在C 中具有指针对象类型
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 字符指针对象和相应的字符数组元素比较
- 如何在 c++ 中克隆为弱指针对象
- 指针对象使用CPP中的指针表达式进行比较
- Visual Studio中智能指针对象的自定义视图
- 使用指针 /对象 /模板参数正确构成
- 如果我确定只有一个线程一次处理指针/对象,则C/C 仍应使用同步
- 如何将值分配给指针对象 c++
- 更改智能指针对象,然后访问它