1

I'm having trouble compiling code with a struct vector. The compiler keeps sending errors, but I cannot locate any. The code is right beneath.

 //2018 USACO Bronze Task 2
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
struct Period{
    int end,starting;
};
int main(){
    ifstream fin("lifeguards.in");
    ofstream fout("lifeguards.out");
    int N;
    fin>>N;
    vector <Period> periods[N];
    for(int i=0;i<N;i++){
        fin>>periods[i].starting>>periods[i].end;
    }
    int record=0,temp=0;
    for(int i=0;i<N-1;i++){
        temp+=periods[i].end-periods[i].starting;
        for(int j=0;j<N;j++){
            if(j==i)continue;
            temp+=periods[j].end-periods[j].starting;
            temp-=max(periods[i].end-periods[j].starting,0);
        }
        if(temp>record)record=temp;
        temp=0;
    }
    fout<<record<<endl;
}

The error message is in the link Errmsg I have checked for any possible grammatical errors that I know of, but it keeps popping out compilation errors. Is there any fix to this?

KevinZheng
  • 21
  • 3
  • 1
    `vector periods[N];` declares an array of vectors. Did you mean `vector periods(N);` to declare a vector with N elements? – 1201ProgramAlarm Jul 21 '21 at 03:13
  • You may want to read this: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Jul 21 '21 at 03:14
  • `vector periods[N];` -- Even if this is what you wanted to do, that line of code is not legal C++. Arrays in C++ must have their size denoted by a compile-time constant, not a runtime value such as `int N;`. If you actually did want an "array of vectors", the proper way would have been `std::vector> periods(N);` – PaulMcKenzie Jul 21 '21 at 03:50
  • @1201ProgramAlarm Yeah I found that, thats the mistake. – KevinZheng Jul 21 '21 at 03:58
  • @AndreasWenzel This is a new account, I can't upload any images yet because of low reputation – KevinZheng Jul 21 '21 at 03:59
  • @KevinZheng: The point of the link I posted is that you should not be using images at all, but that you should be posting the error messages **as text** into the question. Read the link for further information. – Andreas Wenzel Jul 21 '21 at 12:47

1 Answers1

1

lets look at this line:

vector <Period> periods[N];

in this line you defined array of vector so when you call periods[i] this will return one of array item which is vector <Period> so this is returned type not an Period struct it is vector type so it is incorrect. in order to achieve this you should call periods[i][j] or if you mean you want vector with N size you can call reserve method.

N0ll_Boy
  • 500
  • 3
  • 6
  • `vector periods[N];` -- You may also want to point out that this line isn't even legal C++. – PaulMcKenzie Jul 21 '21 at 03:49
  • I see. I changed it to vector periods(N); It's old habits from static array like int arry[N];XD Thx – KevinZheng Jul 21 '21 at 03:50
  • The point I'm making is mentioned in the main comment section. If you actually did want to have an array of vectors, you would use `std::vector> periods(N);`, even though this is not what you really wanted. – PaulMcKenzie Jul 21 '21 at 03:54
  • @PaulMcKenzie It's actually legal. vector periods[N] stands for an array of N vectors of struct Period, and actually works. – KevinZheng Jul 21 '21 at 03:54
  • 1
    No, it is not legal. An array, no matter what type it is, requires a *compile-time* expression to denote the number of entries. The `N` is a runtime variable. Try and compile your code with a compiler that adheres to C++ rules, and you will see that one line of code will not compile. I bet if you started out with a compiler that did this, you never would have posted the question, because you would have had no choice but to figure out a different way than doing `vector periods[N];`. – PaulMcKenzie Jul 21 '21 at 03:54
  • It's actually my mistake, I was trying to initialize a vector of size N... – KevinZheng Jul 21 '21 at 03:55
  • @PaulMcKenzie It is said to be illegal in the textbooks, but believe it or not, programs can run just fine. (You might wanna try it out) – KevinZheng Jul 21 '21 at 03:57
  • 1
    @KevinZheng Microsoft Visual C++ is a textbook? Your code will refuse to compile. [See this](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Basically, you have become another unwilling victim of the `g++` compiler's default option of allowing such non C++ syntax, and you've believed it is valid C++. – PaulMcKenzie Jul 21 '21 at 03:57
  • @PaulMcKenzie My textbook said that as well... but I guess it's the problem of g++. – KevinZheng Jul 21 '21 at 04:03
  • [Here is the compiler error when compiled using Visual C++ 2019](https://godbolt.org/z/nnPeY5Pjd). And [here is the same error when compiled using g++ with the proper flags](https://godbolt.org/z/r5Kb5YE6q). – PaulMcKenzie Jul 21 '21 at 04:04