It was necessary to create a constant QMap with some set of values:

 const QMap<QString,int> my_map{{"field1", 15000}, {"field2", 3000}}; 

On MinGW, there are no problems on Linux, but VC2013 in QtCreator produces an error C2661 .

How can I solve the problem, leaving the code unchanged?

Update 1

Update MS VS 2013 Express to the latest, 4th version did not give a positive result. By booting, it turned out that this error can be solved only by installing the VC compiler 2015. Well, or, of course, through the use of MinGW. Since reinstalling the compiler, along with Qt, as well as other dependent libraries, is not advisable at the moment, you will have to temporarily abandon the use of the designated initialization lists and apply a solution similar to the @yrHeTaTeJlb answer.

Update 2

I forgot to indicate that after updating the compiler to version 4 (in theory, last, I downloaded it from the Microsoft website), the error changed the number to C2797 .

Qt version 5.7.0 for Windows 64-bit (VS 2013)

  • So VS 2012 or 2013? In 2013, my initialization lists work. - 伪位蔚蠂慰位蠀蟿
  • @alexolut, I'll fix it. Worth MS Visual Studio Express 2013, 64 bits - alexis031182
  • In addition to the error number, it would be good to provide more text. - 伪位蔚蠂慰位蠀蟿
  • Unfortunately there are question marks (IDE QtCreator). Since the build in windows has a lower priority, it has not yet been puzzled by the solution of the encoding issue. - alexis031182
  • If you use the English version of VS, then there should be no questions. But this is a somewhat radical decision. - 伪位蔚蠂慰位蠀蟿

3 answers 3

You can do this:

 QMap<QString, int> makeMap(){ QMap<QString, int> result; result["1"] = 2; result["3"] = 4; result["5"] = 6; result["7"] = 8; result["9"] = 0; return result; } const QMap<QString, int> map = makeMap(); 

    Obviously - you need another compiler for Windows. For example, the same MinGW. For example, take the assembly here - for x32 and / or for x64 , later you can build clang . They collect faster.

      I consider that it is inappropriate to create a named function for initialization. Try to do just lambda.

       const QMap<QString,int> map = []() { QMap<QString, int> result; result["1"] = 2; result["3"] = 4; result["5"] = 6; result["7"] = 8; result["9"] = 0; return result; }(); 
      • Do you think that there is support for lambs, but there is no initialization through braces? - 伪位蔚蠂慰位蠀蟿
      • @alexolut, support for lambda is, the proposed @Majestio code is going. My code that is presented in the question is not. In the project file line CONFIG += c++11 is available. - alexis031182
      • @alexolut, judging by msdn.microsoft.com/en-us/library/hh567368.aspx - lambdas appeared in earlier versions of VS, rather than initialization lists. But I never used VS once, so I assumed that it might work. - Majestio