C ++重载operator []

时间:2014-03-05 01:49:12

标签: c++ class operator-overloading runtime-error subscript-operator

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

  

2 IntelliSense:表达式必须包含整数或无范围枚举   类型行:11栏:24本图书馆书籍

  

错误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;
}

我也不是百分百肯定,如果我已正确编码,如果还有错误,请告诉我并谢谢。

4 个答案:

答案 0 :(得分:2)

本声明

    storage = new string[book];

无效。下标值应具有整数或未包含的enueration类型。

您的班级定义似乎有拼写错误,而不是

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

答案 1 :(得分: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成员,则无需将quantity传递给savebooks()savebooks()可以实现为: void savebooks(){ for (int i = 0; i < books; ++i){ cout << "Book: "; getline(cin, storage[i]); } }

答案 2 :(得分: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;
    }

答案 3 :(得分:0)

这是什么?编译器不指示错误的位置吗?

Books(){
        storage = new string[book];
}