我的问题是我的GetTitle()函数没有返回值。
我的班级书的代码已经完成,我仍在完成项目的main()代码并正在测试特定部分。我正在使用用户输入来测试我的函数,看看它们是否有效。
我正在测试的函数是SetTitle()和GetTitle()。我运行代码时没有收到任何错误,但我的GetTitle()函数没有返回值。我已经谷歌搜索并查找其他类似的堆栈溢流问题,只是不明白为什么会发生这种情况。
我运行代码并输入字符串“Test”时的示例:
Enter the book title:
Test
If this is after the book title the test worked
应该输出的是:
Enter the book title:
Test
Test If this is after the book title the test worked
我的代码
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
class Book {
public:
void SetTitle(string title_input);
string GetTitle();
void SetAuthor(string& author_input);
string GetAuthor();
void SetCopyRightYear(int copyright_year_input);
int GetCopyRightYear();
void PrintBook();
private:
string title;
string author;
int copyright_year;
};
void Book::SetTitle(string title_input) {
string title = title_input;
}
string Book::GetTitle() {
return title;
}
void Book::SetAuthor(string& author_input) {
string author = author_input;
}
string Book::GetAuthor() {
return author;
}
void Book::SetCopyRightYear(int copyright_year_input) {
int copyright_year = copyright_year_input;
}
int Book::GetCopyRightYear() {
return copyright_year;
}
void Book::PrintBook() {
cout << "Title of Book: " << GetTitle() << endl;
cout << "Author of Book: " << GetAuthor() << endl;
cout << "Copyright Year: " << GetCopyRightYear() << endl;
}
int main ()
{
string title_input = "";
string author_input = "";
int copyright_year_input = 0;
Book book1;
Book book2;
Book book3;
Book book4;
cout << "Enter the book title: " << endl;
cin >> title_input;
book1.SetTitle(title_input);
cout << book1.GetTitle() << " If this is after the book title the test worked!" << endl;
}
答案 0 :(得分:2)
因为您在setter中创建了一个局部变量并设置了它的值。它与您的对象无关
string title = title_input;
没有设置方法属性的值。将其更改为
title = title_input;
答案 1 :(得分:2)
在函数setTitle中,您正在创建一个隐藏实际标题varibale的局部变量标题,因此不要在它之前使用string
。这种方式改变了每个setter函数。
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
class Book {
public:
void SetTitle(string title_input);
string GetTitle();
void SetAuthor(string& author_input);
string GetAuthor();
void SetCopyRightYear(int copyright_year_input);
int GetCopyRightYear();
void PrintBook();
private:
string title;
string author;
int copyright_year;
};
void Book::SetTitle(string title_input) {
title = title_input;
}
string Book::GetTitle() {
return title;
}
void Book::SetAuthor(string& author_input) {
author = author_input;
}
string Book::GetAuthor() {
return author;
}
void Book::SetCopyRightYear(int copyright_year_input) {
copyright_year = copyright_year_input;
}
int Book::GetCopyRightYear() {
return copyright_year;
}
void Book::PrintBook() {
cout << "Title of Book: " << GetTitle() << endl;
cout << "Author of Book: " << GetAuthor() << endl;
cout << "Copyright Year: " << GetCopyRightYear() << endl;
}
int main ()
{
string title_input = "";
string author_input = "";
int copyright_year_input = 0;
Book book1;
Book book2;
Book book3;
Book book4;
cout << "Enter the book title: " << endl;
cin >> title_input;
book1.SetTitle(title_input);
cout << book1.GetTitle() << " If this is after the book title the test worked!" << endl;
}
答案 2 :(得分:0)
仔细查看您的SetTitle()
功能。您声明一个本地字符串变量,其名称与您的成员字符串变量完全相同。调用函数时,只设置局部变量,而不是成员变量。