5

I have spent well over an hour researching this before I came to you guys for help. I am using visual studio 2012 and I just installed the update 2.

I have this constructor

Lexer::Lexer(istream &source1, ostream& listing1)
:source(source1), listing(listing1)
{
vector<string> tempVec = { 
    "and", "begin", "boolean", "break", "call", "end", "else", "false", "halt",
        "if", "input", "integer", "is", "loop", "not", "null", "newline", "or", "output", "procedure"
        "return", "then", "true", "var"
};




tokenToStringVector = tempVec;

for (int k= 0; k < tokenToStringVector.size(); k++)
{
    string key = tokenToStringVector[k];
    lexemeToTokenMap[key] = Token(k); // Function-style type cast
}
}

My professor wrote the header, and I wrote the code. I am getting this error which I cannot figure out:

1>c:\users\sam\dropbox\compiler project\lexical analyzer\lexer.cpp(8): error C2552: 'tempVec' : non-aggregates cannot be initialized with initializer list
1>          'std::vector<_Ty>' : Types with a base are not aggregate
1>          with
1>          [
1>              _Ty=std::string
1>          ]

I don't understand why it will not let me initialize it like this. I could make an array, and then a loop to add to the vector, but I am already just using the vector to populate a map, so there must be a more logical way to do this.

Thanks SO! You guys are always so helpful!

PLEASE NOTE: If the answer is not a simple formatting error, please don't correct the code, just point me in the right direction. This is a graded assignment and I am an honest student and I want to learn this myself.


Ok, from the replies and re-reading the SO post someone posted.

  1. Visual Studio does not support this, even though it is valid in C++ 2011

Thanks guys, I guess I do just need to make an array.

My Professor told me to do it similar to this and he uses some Linux gcc compiler that does support this.

EDIT: Ok, so Microsoft has an alpha version that supports this. I guess i'll just make an array and transfer it.

THANKS FOR THE HELP!!!!

Kev
  • 118,037
  • 53
  • 300
  • 385
Samuel French
  • 665
  • 3
  • 11
  • 22
  • 5
    It should work. Are you sure you have C++11 support switched on? – juanchopanza Apr 18 '13 at 16:07
  • 2
    http://stackoverflow.com/questions/12654394/initializer-list-not-working-with-vector-in-visual-studio-2012 – stardust Apr 18 '13 at 16:08
  • 2
    By the way, your "procedure" and "return" tokens are being merged into a single const string in the declaration. They are not separated by a **`,`** in the source code. You're gonna need to fix that. – WhozCraig Apr 18 '13 at 16:09

5 Answers5

4

vc++ does not support initializer list.
I do not know whether is is supported in update 2, but the standard library will certainly not support it even if the feature is in.

Edit: update 2 does not support initializer list either.

yngccc
  • 5,594
  • 2
  • 23
  • 33
1

For old C++, the only option is:

const char *constants[] = { "and", "begin", "boolean", "break", "call", "end", "else", "false", "halt",
    "if", "input", "integer", "is", "loop", "not", "null", "newline", "or", "output", "procedure"
    "return", "then", "true", "var" };

vector<string> values(constants,&constants[sizeof(constants)/sizeof(constants[0])]);
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

Your code is valid C++11, but apparently your toolchain is not happy with this (mine is, by the way, Apple LLVM 4.2 Clang). Consider this as an alternative:

Lexer::Lexer(istream &source1, ostream& listing1)
    :source(source1), listing(listing1)
{
    static const char *reserved[] =
    {
        "and", "begin", "boolean", "break", "call",
        "end", "else", "false", "halt", "if", "input",
        "integer", "is", "loop", "not", "null", "newline",
        "or", "output", "procedure", "return", "then",
        "true", "var"
    };

    std::copy(std::begin(reserved), std::end(reserved),
              back_inserter(tokenToStringVector));

    // rest of your code.
}

Note: I took the liberty of fixing the merged "procedure" and "return" strings, which I'm fairly sure you neglected to separate with a , by accident.

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • No need to copy, you can use a static member and initialise the array directly in the initialiser list from that array. – Konrad Rudolph Apr 18 '13 at 17:22
  • @KonradRudolph completely true if the static reserved[] array is class-static and not static to the constructor body (and, in fact, very good advice to do so). I know if `reserved[]` is a class static (or general static) declared outside of the constructor body-scope then assuredly you can use the vector constructor that accepts begin+end iterators for automatic construction. As written, `reserved[]` isn't available for initializer-list construction. – WhozCraig Apr 18 '13 at 18:42
  • Yes, I was implying that in my comment (“static *member* ”). I should have elaborated. – Konrad Rudolph Apr 18 '13 at 18:43
  • @KonradRudolph As should have I when responding while answering the question, not accounting for the fact that is totally a valid (and IMHO correct) alternative. Even if not used by the OP, I shall update the answer at some point to include this better (and correct) construct. Thanks for the input and advice, sir. – WhozCraig Apr 18 '13 at 18:47
0

The error message says the problem. You are trying to initialize a vector of strings with an initializer list, as if it were an array. You can initialize an array with an initializer list, but not a vector. Probably what you want is to create an array from your initializer list, and then initialize the vector from your array.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
0

Syntax var = { x, y, z } can only be used on aggregates and your compiler protest that (for him) vector<string> is not an aggregate.

Community
  • 1
  • 1
Offirmo
  • 18,962
  • 12
  • 76
  • 97
  • And for anybody else `vector` is not an aggregate. That's not the problem. The problem is support for uniform initialization syntax, which was added to the language by C++11. – Pete Becker Apr 18 '13 at 16:15