-3

I have the following code, where I have created a constructor for my struct and I am trying to assign the a struct to a particular index of a vector container.

I am getting an error, but shouldn't it be possible to do something like this?

#include<bits/stdc++.h>
using namespace std;

struct vals{
    int a;
    int b;
    int c;
    vals(int x, int y, int z)
        :a(0), b(0), c(0)
    {
        a=x;b=y;c=z;
    }
};

vals f(int n, vector<vector<int>> &v)
{
    vector<vals> dp(n+1);
    dp[1]=vals(v[1][0], v[1][1], v[1][2]);

    for(int i=2; i<=n; i++)
        dp[i] = vals(max(dp[i-1].b, dp[i-1].c)+v[i][0], max(dp[i-1].a, dp[i-1].c)+v[i][1], max(dp[i-1].a, dp[i-1].b)+v[i][2]);
    return dp[n];
}


int main()
{
  int n;
  cin >> n;
  int numAct = 3;
  vector<vector<int>> v(n+1, vector<int>(numAct));
  for(int i=1; i<=n; i++)
    for(int j=0; j<3; j++)
      cin >> v[i][j];

  vals res = f(n, v);
  cout << max(res.a, max(res.b, res.c)) << endl;
}

For anyone wanting to understand the code, this is the problem I am trying to solve : link

Karan Singh
  • 1,114
  • 1
  • 13
  • 30
  • 2
    `v` is a `vector`. So `v[1]` will give you an `int`. And then you do `[0]` on that int...what are you trying to do there? (this is the nice human version of what the compiler is telling you) – scohe001 Aug 16 '19 at 19:24
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h –  Aug 16 '19 at 19:25
  • `#include` probably not – Justin Randall Aug 16 '19 at 19:25
  • 6
    `#include` [loads the gun](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). `using namespace std;` [takes off the safety](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Be very careful with this. – user4581301 Aug 16 '19 at 19:26
  • @scohe001 sorry, I've corrected the mistake I made. – Karan Singh Aug 16 '19 at 19:27
  • @user4581301 I know this is bad practice. But in competitive programming, I almost always see people doing this, so got into this habit to save time. – Karan Singh Aug 16 '19 at 19:29
  • In order for a `vector` to be able to size or `resize` itself it must be able to default construct the structure it contains. `vals` cannot be default constructed. – user4581301 Aug 16 '19 at 19:29
  • 1
    related/dupe for your new error: https://stackoverflow.com/questions/29920394/vector-of-class-without-default-constructor – NathanOliver Aug 16 '19 at 19:31
  • 1
    @KaranSingh you should see what including the entire standard library does to your build times. On my box this takes 9s.410ms. With the proper headers, 782ms. You make that savings on typing back in at most 2 compiles. – user4581301 Aug 16 '19 at 19:32
  • Sidenote: Since you are competing, know that `vector>` can demonstrate some very bad caching behaviour. Consider using a 1D vector and computing the 1D to 2D mapping by hand. Example: https://stackoverflow.com/a/2076668/4581301 – user4581301 Aug 16 '19 at 19:34
  • `#include using namespace std;` - No, no, no. Don't do this crap. What's *wrong* with people.. – Jesper Juhl Aug 16 '19 at 19:58

1 Answers1

3

Your error is right here:

vector<vals> dp(n+1);

But why? Aren't you just harmlessly creating a vector of n+1 vals? Well, that's what you're trying to do at least.

But the second that you create your own constructor for vals--poof!--you no longer get a default constructor automagically created for you.

And guess which constructor you're trying to call to instantiate all those vals for your vector? The default one! That doesn't exist!

To resolve this, you can create a default constructor explicitly, or force the vector to call your constructor when creating the vals:

vector<vals> dp(n+1, vals(0,0,0));
scohe001
  • 15,110
  • 2
  • 31
  • 51