Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initilizing a vector of strings where I already know all the values to populate it with C++ [duplicate]

Tags:

c++

vector

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!!!!

like image 260
Samuel French Avatar asked Apr 18 '13 16:04

Samuel French


1 Answers

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.

like image 87
yngccc Avatar answered Sep 20 '22 23:09

yngccc