0

入力したい本の量を入力し、オーバーロードされた演算子 ([]) を使用するアプリケーションを作成しましたが、配列を格納するためのポインターを与えるたびに、次のようなエラーが発生します。

2 IntelliSense: 式には整数型またはスコープなしの列挙型が必要です。行:11 列:24 図書館の本

エラー 1 エラー C2440: 'initializing' : '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

1

私の提案:

  1. 本の数を示すものは何もありませんBooks。おそらく、`Books' のコンストラクタで本の数を渡したいと思うでしょう。 Books(int numBooks) { storage = new string[numBooks]; }
  2. If you want to store the number of books in Books, add member. There is no use for the member books. Perhaps you intended to use books to store the number of books. If you decide to do that, change string books; to int books; and make sure you initialize books in the constructor. Books(int numBooks) : books(numBooks) { storage = new string[numBooks]; }
  3. If you decide to store the number of books as a member of Books, there is no need to pass quantity to savebooks(). savebooks() can be implemented as: void savebooks(){ for (int i = 0; i < books; ++i){ cout << "Book: "; getline(cin, storage[i]); } }
于 2014-03-05T02:10:58.767 に答える
0

これは何ですか?コンパイラはエラーの場所を示しませんか?

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