-1

I read the article Initializing a vector of vectors having a fixed size with boost assign which should exactly meet my demands: initializing a matrix-like vector of a vector that can be arbitrarily expanded in both directions (I want to use it to extract and group a selection of values out of a bigger list).

However, the solution given in the first 2 answers

    vector<vector<int>> v(10, vector<int>(10,1));        

prompts a syntax error in my CDT_eclipse and the following error from my compiler:

     error: expected identifier before numeric constant
     vector <vector <int> > v(10, vector <int>(10,1));

--

The version found in vector of vector - specific syntax works for my eclipse:

     vector<vector<int>> v = vector<vector<int>>(n, vector<int>(n, 0));        

It prompts however a warning from my compiler:

    vector warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11  [...]

Changing the compiler version (gcc 5.4.0 20160609 for Ubuntu 5.4.0-6ubuntu1~16.04.10) is not possible in the grand scheme where my code is supposed to be used. So I need a compatible formulation of the upper mentioned command. Thanks a lot!

EDIT: My two main attempts looked like this:

    vector <vector <int> > v(10, vector <int>(10,1));   --> syntax error
    vector <vector<int> > v = vector <vector<int> >(1, vector<int>(1, 0));   --> compiler error
BlueFire
  • 3
  • 4
  • Compiles just fine in my case. Have you tried `std::vector> v(10, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });`? – Aykhan Hagverdili Nov 13 '18 at 14:41
  • 1
    1) Since you get a warning regard compiling with -std=c++11 flag, did you try setting it? 2) If I remember correctly, pre C++-11 `vector> v(10, vector(10,1));` must be `vector > v(10, vector(10,1));`. – Algirdas Preidžius Nov 13 '18 at 14:43
  • @Ayxan this unfortunately leaves me with another syntax error and my compiler complains as well: error: expected identifier before numeric constant – BlueFire Nov 13 '18 at 14:51
  • @AlgirdasPreidžius: 1) the compilation is done by a separate script, that I cannot change (our institutional structure is quite complicated...). so I did not try to set a flag, but unfortunately cannot either :/ 2) I included the additional spaces between >> but that is not the problem. – BlueFire Nov 13 '18 at 14:57
  • @BlueFire "_I included the additional spaces between >> but that is not the problem._" In that case you are not compiling the code, that you are showing us. [It compiles fine even with C++03.](https://wandbox.org/permlink/8lAHqJQqGpXs3xNM) – Algirdas Preidžius Nov 13 '18 at 15:01
  • @AlgirdasPreidžius sorry, i just added my code at the bottom. – BlueFire Nov 13 '18 at 15:02
  • @BlueFire [Both of those compile fine with C++03](https://wandbox.org/permlink/0mlhMWbsePQX2FsI). – Algirdas Preidžius Nov 13 '18 at 15:05
  • same [with c++98](http://coliru.stacked-crooked.com/a/2ed3206327f3c563)... – user1810087 Nov 13 '18 at 15:06

1 Answers1

0

I will guess here that you are forgetting to tell us that you are trying to declare a data member for a class. So you are really trying to compile something like:

struct A {
    vector <vector <int> > v(10, vector <int>(10,1));       
};

and

struct A {
    vector <vector<int> > v = vector <vector<int> >(1, vector<int>(1, 0));
};

A data member can only be initialized in a class definition with an equal sign or with braces. The initialization with parentheses is not allowed.

Try:

struct A {
    vector<vector<int>> v{10, vector<int>(10,1)};        
};

In any case you need at least C++11 for the brace-initialization, as well as non-static member initialization in the declaration (your second error where you use equals instead of parentheses, but with older C++ standard).

  • I think so, but will try to make it clearer: For an existing simulation program i want to write additional functionalities serving a slightly different purpose than the original one. Therefore I will add a header and a cpp-file including all functions needed, that can easily be merged with the current code. In the header file I want to declare the (public) vector-matrix so that I can use it in the .cpp-file and reference it later elsewhere. All the additional functions are stored in one class `void className::functionName(ReferenceA, ReferenceB)` – BlueFire Nov 14 '18 at 16:29
  • unfortunately your suggestion did not work either. But since this problem seems to rely solely on the old compiler version, I will try to convince my superiors to make the necessary updates... Thanks (all) for your help ! – BlueFire Nov 14 '18 at 16:32
  • @BlueFire To be clear: The vector is part of the public interface of the class, right? It is not a global variable, right? As I said, you need to use a compiler supporting C++11 (e.g. with `-std=c++11`, which gcc 5.4 does however), otherwise what you want to do is impossible. The only way to do this in the older C++ standards is by defining a (default) constructor for the class, setting the default values for the members. –  Nov 14 '18 at 17:08