2

im having trouble with my compiler

Im using codeblocks with gnugcc

and i want to do:

table.push_back({""});

and it says

main.cpp|22|error: expected primary-expression before '{' token|

code works in VS and other compilers... What the hell?

vector < car > AddCar()
{
    int i;
    vector < car > table;

    string check;
    table.push_back({""});

    for (i = 0; i < table.size(); i++)
    {
        cout << "marka: ";
        cin >> table[i].mark;
        cout << "model: ";
        cin >> table[i].model;
        cout << "cena: ";
        cin >> table[i].price;
        cout << endl;
         table.push_back(car());
   ...

yes i want an empty pushback

Gadzin
  • 73
  • 7

1 Answers1

3

This error is occurring because you probably do not have C++11 enabled. For example, consider this program:

#include <iostream>
#include <string>
#include <vector>

struct Car {
    std::string a;
};

int main() {
    std::vector<Car> example;

    example.push_back({""});
}

When run with C++98 in GCC 8.2.0, the error shown is:

prog.cc: In function 'int main()':
prog.cc:12:23: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
     example.push_back({""});
                       ^
prog.cc:12:27: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
     example.push_back({""});
                           ^

Running this with C++11 fixes the error.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88