私の問題は、私のGetTitle()関数が値を返さないということです。クラスメンバ関数getは値を返しません
私のクラスBookのコードが完成しました。私はまだプロジェクトのmain()コードを完成させていて、特定の部分をテストしていました。私は自分の関数をユーザー入力でテストして、彼らがどのようにしなければならないかを調べました。
私がテストしている機能は、SetTitle()とGetTitle()です。コードを実行するとエラーは発生しませんが、GetTitle()関数は値を返しません。私はグーグルと同様の他のスタックオーバーフローの質問を見上げて、ちょうどこれが起こっている理由を見ることができません。私は、コードの入力を実行したときに
たとえば、文字列 "テスト":
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;
}
/home/ubuntu/workspace/lab25/lab25.cpp:52:13:エラー: 'title_input'がこのスコープで宣言されていませんでした。これは私がこれを行うときのエラーです。 –
@testtestエラーがセッター行または他の行にあることを確認できますか? –