奇怪的是,当我输入要存储的成员的标题时,并没有存储它的问题。我不知道问题是什么,我已经尝试了很多东西。我知道中值变量可以正常工作,但实际的类成员函数存储不起作用,请帮助。

 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,该变量隐藏类成员。当函数超出范围时,将不保留该值。

它看起来应该像这样:

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

关于c++ - 为什么我的SetBookTitle不存储字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28076318/

10-11 23:02
查看更多