-2

I am trying to implement the fractional knapsack famous problem. I needed a struct to connect the values and the weights. Now I want to read the array of struct item, but it gives me this:

invalid expression error

#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;
}
Robert
  • 7,394
  • 40
  • 45
  • 64
Ahmed Hossam
  • 23
  • 1
  • 6

1 Answers1

1

arr is a vector of objects of type Item. To access Item fields, you have to do so using . or -> if you are using a pointer. Using cin >> arr[i] you are trying to input a char to an object of Item.

Try this: std::cin >> arr[i].value

Segmentation
  • 403
  • 7
  • 20