2017-10-16 10 views
0

私の問題は、私の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; 
} 

答えて

2

なぜなら、セッターにローカル変数を作成し、その値を設定しているからです。あなたのオブジェクトとは関係がありません

string title = title_input; 

このメソッドの属性の値は設定されていません。

title = title_input; 
+0

/home/ubuntu/workspace/lab25/lab25.cpp:52:13:エラー: 'title_input'がこのスコープで宣言されていませんでした。これは私がこれを行うときのエラーです。 –

+0

@testtestエラーがセッター行または他の行にあることを確認できますか? –

0

​​の機能を注意深く見てください。メンバーstring変数とまったく同じ名前のローカル文字列変数を宣言しています。関数が呼び出されると、メンバ変数ではなくローカル変数のみが設定されます。

+0

私はあなたが言っていることを理解していると信じています。どうすればこの問題を解決できますか?私は参照渡しを使用しなければならないと信じていましたが、以前に試してみるとエラーが発生します。 –

2

関数setTitleでは、実際のタイトル変数を隠すローカル変数タイトルを作成していますので、その前にstringを使用しないでください。そして、この方法はすべてのセッター機能を変更します。

#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; 
} 
関連する問題