我制作了一个应用程序,您可以在其中键入要输入的书籍数量并使用重载运算符([])但是每次我给出一个存储数组的指针时,它都会给我一个错误,例如
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% 确定我是否正确编码,如果还有错误,请告诉我,谢谢。