所以我使用默认构造函数将单个条目插入内存,然后通过调用getBookInfo()
方法读取它们。当我尝试只使用一个变量进行测试运行时,即使我getBookInfo()
在插入数据后调用,我也没有得到任何东西。
这是为什么?
主文件
#include <iostream>
using namespace std;
#include "Book.h"
void main()
{
Book book;
book.setTitle("Advanced C++ Programming");
book.setAuthorName("Linda", "Smith");
book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
book.setPrice(49.99);
book.getBookInfo(); // <-= this should be output
int i;
cin >> i;
};
书本.cpp
#include <iostream>
#include <sstream>
using namespace std;
#include "Book.h"
Book::Book()
{
}
void Book::setTitle(string title)
{
title = title;
}
void Book::setPrice(double price)
{
price = price;
}
string Book::convertDoubleToString(double number)
{
return static_cast<ostringstream*>( &(ostringstream() << number) ) -> str();
}
// this should be output
string Book::getBookInfo()
{
stringstream ss;
ss << title << endl << convertDoubleToString(price) << endl;
return ss.str();
}