0

我制作了一个应用程序,您可以在其中键入要输入的书籍数量并使用重载运算符([])但是每次我给出一个存储数组的指针时,它都会给我一个错误,例如

2 IntelliSense:表达式必须具有整数或无范围枚举类型 Line:11 Column:24 Library Books

错误 1 ​​错误 C2440:“正在初始化”:无法从“std::string”转换为“unsigned int”行:11 列:1 图书馆书籍

但无论如何这是我的代码:

#include <iostream>
#include <string>
using namespace std;

class Books{
private:
    string* storage;
    string book;
public:
    Books(){
        storage = new string[book];
    }
    void savebooks(int iterate){
        for (int i = 0; i < iterate; ++i){
            cout << "Book: ";
            getline(cin, storage[i]);
        }
    }

    const string operator[](const int ref){
        return storage[ref];
    }
    ~Books(){
        delete storage;
    }
};

int main(){
    //local variables 
    int quantity;
    //user display
    cout << "Welcome to Book Storage Viewer" << endl;
    cout << "How many books would you like to insert: ";
    cin >> quantity;
    //instantiante 
    Books bk;
    //other handle
    bk.savebooks(quantity);
    //display books
    cout << "These are the books you've entered" << endl;
    for(int i = 0; i < quantity; ++i){
        cout << bk[i] << endl;
    }
    system("pause");
    return 0;
}

我也不是 100% 确定我是否正确编码,如果还有错误,请告诉我,谢谢。

4

4 回答 4

2

这个说法

    storage = new string[book];

是无效的。下标值应具有整数或无范围枚举类型。

看来您的类定义有错字,而不是

string book;

应该有例如

int book;

我认为您的意思如下

private:
    string* storage;
    int quantity;
public:
    Books( int quantity) : quantity( quantity ) {
        storage = new string[this->quantity];
    }
    void savebooks(){
        for (int i = 0; i < quantity; ++i){
            cout << "Book: ";
            getline(cin, storage[i]);
        }
    }
//...

主要应该有

int quantity;
//user display
cout << "Welcome to Book Storage Viewer" << endl;
cout << "How many books would you like to insert: ";
cin >> quantity;
//instantiante 
Books bk();
bk.savebooks();
于 2014-03-05T01:54:38.880 回答
1

我的建议:

  1. 没有任何内容Books表明有多少本书。也许您想在“书籍”的构造函数中传递书籍的数量。 Books(int numBooks) { storage = new string[numBooks]; }
  2. 如果要在 中存储图书数量Books,请添加成员。会员没有用books。或许你打算用来books存放书籍的数量。如果您决定这样做,请更改string books;int books;并确保books在构造函数中进行初始化。 Books(int numBooks) : books(numBooks) { storage = new string[numBooks]; }
  3. 如果您决定将图书数量作为会员存储Books,则无需传递quantitysavebooks()savebooks()可以实现为: void savebooks(){ for (int i = 0; i < books; ++i){ cout << "Book: "; getline(cin, storage[i]); } }
于 2014-03-05T02:10:58.767 回答
1

似乎没有人提到程序结束时的段错误,delete storage但应该是delete [] storage

我试图编译代码,当书籍少于 100 时,这个是可以的。

   #include <iostream>
    #include <string>
    using namespace std;

    class Books{
    private:
        string* storage;
        string book;
    public:
        Books(){
            storage = new string[100];
        }
        void savebooks(int iterate){
            for (int i = 0; i < iterate; ++i){
                cout << "Book: ";
                getline(cin, storage[i]);
            }
        }

        const string& operator[](const int ref){
            return storage[ref];
        }
        ~Books(){
            delete [] storage;
        }
    };

    int main(){
        //local variables 
        int quantity;
        //user display
        cout << "Welcome to Book Storage Viewer" << endl;
        cout << "How many books would you like to insert: ";
        cin >> quantity;
        //instantiante 
        Books bk;
        //other handle
        bk.savebooks(quantity);
        //display books
        cout << "These are the books you've entered" << endl;
        for(int i = 0; i < quantity; ++i){
            cout << bk[i] << endl;
        }
        return 0;
    }
于 2014-03-05T02:15:48.220 回答
0

这是什么?编译器没有指出错误在哪里吗?

Books(){
        storage = new string[book];
}
于 2014-03-05T01:53:40.980 回答