【问题标题】:why is my SetBookTitle not storing the strings为什么我的 SetBookTitle 不存储字符串
【发布时间】:2015-01-21 20:34:48
【问题描述】:

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

 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);

【问题讨论】:

  • string BookTitle = BT; 这是创建一个与您的成员变量同名的变量并将 BT 分配给它。删除类型:BookTitle = BT;
  • 请看一本好书(涵盖C/C++)

标签: c++ arrays string class variables


【解决方案1】:
void SetBookTitle(string BT)
{
    string BookTitle = BT; //Does not assign to class member
}

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

它应该看起来更像这样:

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

【讨论】:

  • 养成对成员和堆栈变量使用不同命名约定的习惯也非常有用。例如,在成员前面加上 "m_" -> m_someVarName 是很常见的。
  • 哇,好吧,现在实际发生的事情以前没有发生过
  • 您好,同样的事情发生在我的出版商、ISBN、价格和作者身上。主要是作者和出版商
  • @LegacyWink 问题是一样的...取出前面的 type (这使它成为函数范围内的新变量)。您可能还想对局部变量和类成员使用不同的大小写或命名约定。通过在for 循环本地创建一个新的未初始化 tracka,您的setauthor 也是错误的。
【解决方案2】:

您正在重新定义变量名。

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

应该是:

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

我倾向于使用this-&gt; 来确保我使用的是班级成员。

【讨论】:

  • 好的,谢谢,在书名弄乱之前我没有意识到字符串
猜你喜欢
  • 1970-01-01
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2020-07-26
  • 2018-03-26
  • 1970-01-01
  • 2019-09-02
相关资源
最近更新 更多