无效操作数以二进制读取结构的阵列
invalid operand to binary expression reading an array of struct
我正在尝试实现分数符号著名问题。我需要一个结构来连接值和权重。现在我想阅读结构项目的数组,但是它给了我这一点:
无效的表达式错误
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using namespace std;
// Structure for an item which stores weight and corresponding
// value of Item
struct Item
{
int value, weight;
// Constructor
Item(int value, int weight) : value(value), weight(weight) {}
};
int main()
{
int n;
int W;
std::cin >> n >> W;
vector<Item> arr(n);
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
cout << "Maximum value we can obtain = " << fractionalKnapsack(W, arr, n);
return 0;
}
arr
是类型Item
对象的vector
。要访问Item
字段,如果您使用的是pointer
,则必须使用.
或->
。使用cin >> arr[i]
,您正在尝试将char
输入到Item
的对象。
尝试以下操作:std::cin >> arr[i].value
相关文章:
- 如何将来自文本文件的数据读取到结构的int阵列中
- 将数据从阵列火阵列传输到犰狳结构
- 将阵列的元素变为结构
- c/c qsort在结构内的结构阵列
- 生态2D结构阵列C
- 如何计算结构阵列内的平均值
- 工会结构阵列的初始化太多
- 如何在C 中创建动态分配的2D结构阵列
- 动态分配的结构阵列具有动态分配的结构阵列元素
- 传递和更新结构阵列
- C 98如何在标题中初始化静态结构阵列
- C 结构阵列带指针
- 用null C 初始化结构阵列
- 结构阵列奇数/偶数故障
- 排序二维结构阵列
- 如何确定另一个结构内的结构阵列的长度
- 结构阵列的初始化列表的行为
- 在结构阵列(AoS)和阵列结构(SoA)之间来回切换
- 2D结构阵列
- 用于将结构阵列(AoS)转换为阵列结构(SoA)的干净代码