0

I'm trying to create simple database engine. I have a problem with structures.

#include <iostream> #include <vector> #include <string> using namespace std; struct LICZBY { int wartosc; }; struct STUDENCI { int indeks; string imie; string nazwisko; }; struct PRZEDMIOTY { int id; string nazwa; // auto int semestr; // clamp (1/10) }; struct SALE { string nazwa; int rozmiar; // clamp (10/600) bool projektor; double powierzchnia; }; struct TABLES { vector<LICZBY> liczby; vector<STUDENCI> studenci; vector<PRZEDMIOTY> przedmioty; vector<SALE> sale; }; int main() { TABLES tables; tables.liczby.push_back({1}); cout << tables.liczby[0].wartosc; // your code goes here return 0; } 

I'm using Visual Studio 2012. This code return an error: expected an expression (here tables.liczby.push_back({1});), but code works at ideone.com. http://ideone.com/fork/zc9pz8

What is wrong? Please give me some advise.

1
  • 3
    Not yet supported in VS2012, available in VS2013. Commented Dec 3, 2013 at 23:10

2 Answers 2

3

VS2012 does not yet support C++11 uniform initialization syntax/semantics, which is what's required for your {1} to work in this context.

  • At this time it cannot be rewritten as a one-liner for VS2012 (unless you declare a conversion constructor in your LICZBY class). For the original definition of LICZBY you can only rewrite it as

    const LICZBY liczby = { 1 }; tables.liczby.push_back(liczby); 
  • If you add a conversion constructor

    struct LICZBY { int wartosc; LICZBY(int wartosc) : wartosc(wartosc) {} }; 

    then you'll be able to do it as

    tables.liczby.push_back(1); 
Sign up to request clarification or add additional context in comments.

Comments

1

This line:

tables.liczby.push_back({1}); 

is trying to use an initializer list. This is supported by the gcc version used on ideone, but is not supported by VS2012. In this case, it's an easy fix:

tables.liczby.push_back(LICZBY(1)); 

1 Comment

Easy fix? This will not compile. Class LICZBY has no constructor. Expression LICZBY(1) is invalid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.