0

I'm having a weird issue where the variables i'm declaring that are pushed back to a vector<vector<pixel>> are not being actually pushed back... Take a look:

vector<pixel> Lignetemp;
for(int j = 0; j < InfoSup.nbL; j++){
    Lignetemp.clear();
    for(int i = 0; i < InfoSup.nbC; i++){
        int Rtemp, Vtemp, Btemp;
        cin >> Rtemp;
        cin >> Vtemp;
        cin >> Btemp;
        Lignetemp.push_back({Rtemp,Vtemp,Btemp});
    }
    Data.push_back(Lignetemp);
}

Pixel is defined as such:

struct pixel{
    int R, V, B; 
};

The issue is that when I try to print out Data, I get only 0's instead of the values that I cin Also, I'm coding on both Windows and Linux ( g++ compilers of different versions) and namely on Ubuntu the compiler gives me these sort of warnings:

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11

Do you have an idea about a fix ? Thanks :)

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Falanpin
  • 55
  • 12
  • 2
    Looks like it should work. Can we get a compile-able [mcve]? – NathanOliver Dec 04 '18 at 21:53
  • It should work, but as an aside, if you restricted the scope of `Lignetemp` to the inner loop, you wouldn't have to clear it. It is generally good practice to restrict scope as much as possible anyway. – Mike Borkland Dec 04 '18 at 21:58
  • @NathanOliver prior to C++11 `v.push_back({a,b,c})` was a syntax error; in this case (the type being an aggregate) you had to do `T t(a, b, c); v.push_back(t);` – M.M Dec 04 '18 at 22:00
  • @M.M Sure, but it compiling and running on windows so it must be doing something. The op just needs to turn on c++11 on their linux build. – NathanOliver Dec 04 '18 at 22:02
  • 1
    Psychic debugger says: `cin` has the `fail` bit set – Mooing Duck Dec 04 '18 at 22:12
  • @NathanOliver I've implemented those changes but nothing new ... Thank you however, this is getting frustrating – Falanpin Dec 05 '18 at 07:52

2 Answers2

2

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11

The warning gives you a clue.

You pass the initializer list to the push_back method. Here is it: {Rtemp,Vtemp,Btemp}

You can either:

  1. Pass the flag, mentioned in the warning, to your compiler. How to pass it - depends on what is your build system.

  2. Use explicit instantiation:

    pixel p;
    cin >> p.R >> p.V >> p.B;
    Lignetemp.push_back(p);
    
  • 2
    They could just do `cin >> p.R >> p.V >> p.B;` instead of having the temp ints – M.M Dec 04 '18 at 22:05
1

Do as the warning suggests and add -std=c++11 or -std=gnu++11 to your compiler options.

The syntax Lignetemp.push_back({Rtemp,Vtemp,Btemp}); was not added to the language until C++11.

M.M
  • 138,810
  • 21
  • 208
  • 365