为什么我的 SetBookTitle 不存储字符串

why is my SetBookTitle not storing the strings

奇怪的是,当我输入一个标题供会员存储时,它并没有存储它,这是怎么回事。我不知道问题出在哪里我已经尝试了很多东西。我知道中值变量工作正常但实际 class 成员函数存储不起作用请帮忙。

 class BookType
{
private:
    string Bookinfo;
    string BookTitle;
    string publisher;
    double price;
    int ISBN;
    string authors[4];
    int tracka;
    int copy;
    int Booktracker;

public:
    BookType()
    {

    }
    void setpub(const std::string &pr)
    {
         publisher = pr;
    }
    string getpub()
    {
        return publisher;
    }
    void setcopy(int c)
    {
    int copy = c;
    }
    int getcopy()
    {
        return copy;
    }
    void setprice(double p)
    {
    double price = p;
    }
    double getprice()
    {
        return price;
    }
    void SetBookTitle(const std::string &BT)
    {
        BookTitle = BT;
    }
    string GetBookTitle()
    {
        return BookTitle;
    }
    void SetISBN(int I)
    {
         ISBN = I;
    }
    int GetISBN()
    {
        return ISBN;
    }
    void setnumAuthor(int a)
    {
        tracka = a;
    }
    void setauthor(const std::string &a, int tracka)
    {
        for (int tracka; tracka<4; tracka++)
        {
            authors[tracka] = a;
        }
    }
    void setTBook(int T)
    {
        Booktracker = T;
    }
    string getauthor()
    {
        for (int i = 0; i < 4; i++)
        {
            return authors[i];
        }
    }
};

int main()
{



    optionmenu();


    system("pause");
        return 0;
}

void optionmenu()
{
    BookType Book[10];
    int a = 0;
    string title;
    int isbn;
    int i = 0;
    string nu;
    string author;
    char choice;
    string pub;
    double price;
    int copy;
    int ch;
    string srch;
    bool found = false;

    cout << "Hello welcome to the Bookinfo site please select an option." << endl;
    cout << " do you wish to continue the book info program? 1 for yes any key for no!" ; cin >> ch;
    while (ch == 1)
    {

    MenuOptions();
    cin >> choice;

    switch (choice)
    {

    case '1': 

    while (i >= 10)
    {
        cout << "you have reached he book limit, choose another menu option!"; cin >> choice;
    }
    cout << i << endl;

    cout << i << endl;
        cout << "=====================" << endl;
        getline(cin, nu);
        cout << "Please enter book title" << endl;
        getline(cin,title);
    Book[i].SetBookTitle(title);
if (choice = '1')
    {
        i++;
    }

        cout << "please enter the author(s) name(s), if less then 4 authors leave (NA) in the leftover blanks." << endl;
        for (int ai = 0;  ai < 4; ai++)
        {
        getline(cin, author);   
        Book[i].setnumAuthor(ai);
        }
        cout << "=======================================================" << endl;
        cout << "please enter the books ISBN Number." << endl;
        cin >> isbn;
        Book[i].SetISBN(isbn);


        cout << "what is the publisher of the book?" << endl;
        getline(cin, nu);
        getline(cin, pub);

        Book[i].setpub(pub);
void SetBookTitle(string BT)
{
    string BookTitle = BT; //Does not assign to class member
}

这将创建一个新的函数局部变量 BookTitle 隐藏 class 成员。当函数超出范围时,该值将不会保留。

它应该看起来更像这样:

void SetBookTitle(const std::string &BT)
{
    BookTitle = BT;
}

您正在重新定义变量名。

void SetBookTitle(string BT)
{
    string BookTitle = BT;
}

应该是:

void SetBookTitle(string BT)
{
    BookTitle = BT;
}

我倾向于使用 this-> 来确定我使用的是 class 成员。