[英]I'm not getting any output after I have inserted data by default constructor but I'm not sure why not
So I am using default constructor to insert individual entries into memory and then read them by calling getBookInfo()
method. 因此,我使用默认构造函数将单个条目插入内存,然后通过调用
getBookInfo()
方法读取它们。 When I try just to do a test run with only one variable I am not getting anything there even if I am calling getBookInfo()
after I inserted the data. 当我尝试仅使用一个变量进行测试运行时,即使在插入数据后调用
getBookInfo()
,也没有得到任何结果。
Why is that? 这是为什么?
Main.cpp Main.cpp
#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;
};
Book.cpp Book.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();
}
Change 更改
void Book::setTitle(string title)
{
title = title;
}
to 至
void Book::setTitle(string title)
{
this->title = title;
}
and the same change wherever this kind of code occurs. 并且无论发生这种代码的地方都发生了相同的变化。 As written, the code assigns the value of the argument
title
to the argument title
, ie it does nothing. 如所写,代码将参数
title
的值分配给参数title
,即不执行任何操作。 The compiler probably warned you about this. 编译器可能对此发出警告。
Or, as @hmjd said, change the names of the arguments. 或者,如@hmjd所说,更改参数名称。
You need to capture the data returned from getBootInfo(), for example 例如,您需要捕获从getBootInfo()返回的数据。
string result = book.GetBookInfo();
cout << result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.