【发布时间】:2016-02-23 22:59:21
【问题描述】:
- 我是 C++ 新手。编译器抱怨以下行: inv.inventory[0] = "书籍"。请帮助我如何分配值 到 Class 中的静态数组。
// 类声明
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Items
{
private:
string description;
public:
Items()
{
description = ""; }
Items(string desc)
{
description = desc;}
string getDescription() { return description; }
};
class InventoryItems {
public:
static Items inventory[5];
};
// main function
int main()
{
const int NUM = 3;
InventoryItems inv;
inv.inventory[0] = "Books";
for (int i = 0; i < NUM; i++)
{
cout << inv.inventory[i].getDescription() << endl;
}
return 0;
}
我遇到以下错误:
invMain.cpp:31: 错误: InventoryItems::inventory[0] = "Books" 中的 operator= 不匹配 invMain.cpp:7: 注意:候选者是:Items& Items::operator=(const Items&)
【问题讨论】: