Trying to Create an array holding a Struct in C++?

I am trying to create an array using a struct in c++ which takes two variables i.e value and weight. So I created an array which will have value and weight in one element like this Arr[]={{1,2},{3,4}}...and i want that if i called Arr[0].value and Arr[0].weight then it should return 1 and 2 respectively but I think I'm doing something wrong because im getting many errors

    //Heres my Item struct....
    
    struct Item
    {
        int value, weight;
        // Constructor
        Item(int value, int weight)
        {
            this->value = value;
            this->weight = weight;
        }
    };



    //This is my knapsack function

    double knap(int n, Item arr[], double w)
    {
        double v = 0;
        double am = 0;
        for (int i = 0; i < n; ++i)
        {
            if (w == 0)
            {
                return v;
            }
            am = min(arr[i].weight, w);
            v += am * (arr[i].value / arr[i].weight);
            w -= am;
        }

        return v;
    }

    //Heres my main() function

    int main()
    {
        int n;
        double w;
        cin >> n >> w;

        struct Item arr[n];


        for (int i = 0; i < n; ++i)
        {
            cin >> arr[i].value >> arr[i].weight;
        }
        //this is a fuction i want to run
        cout << knap(w, arr[], n);
    }

Here are the errors

  /storage/emulated/0/coursera/max money2.cpp:50:14: 
   errorr: no matching constructor for initialization of 
   'structt Item [n]'
        struct Item arr[n];
                    ^
    /storage/emulated/0/coursera/max money2.cpp:7:9: note: 
    candidatee constructor (the implicit copy constructor) 
   not viable: requires 1 argument, but 0 were provided
        struct Item
               ^
    /storage/emulated/0/coursera/max money2.cpp:7:9: note: 
   candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
/storage/emulated/0/coursera/max money2.cpp:11:3: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
                Item(int value, int weight)
                ^
    2 errors generated.

1 answer

  • answered 2021-05-15 03:58 selbie

    Your biggest compile issues are this:

    You got to give your Item a default constructor, otherwise, it can't exist in an uninitialized array.

    struct Item
    {
        int value, weight;
        // Constructor
        Item(int value, int weight)
        {
            this->value = value;
            this->weight = weight;
        }
    
        Item() : value(0), weight(0)
        {};
    };
    

    It appears you got your parameters reversed when you invoke knap like this. Also, you pass the array without the brackets:

        cout << knap(w, arr[], n);
    

    You probably meant:

        cout << knap(n, arr, w);
    

    As others have pointed out with this:

        struct Item arr[n];
    

    That's not valid C++, but g++ allows it.

    Better:

        std::vector<Item> arr;    // #include <vector>
        arr.resize(n);
    

    Then you pass it as an pointer to your knap function (which is written to expect it this way:

    cout << knap(n, arr.data(), w);
    

    Or alternatively, modify your knap function to take a vector:

    double knap(vector<Item>& arr, double w)
    {
        int n = arr.size();
    

    So you can say:

    cout << knap(arr, w);