Reputation: 9708
How do I fix this to get this to compile?
#include <utility>
int main() {
const std::pair<const char*, const char*> pairs[] = { {"String A", "String 1"},
{"String B", "String 2"}
};
}
Giving this compiler error:
1>main.cpp(256): error C2552: 'pairs' : non-aggregates cannot be initialized with initializer list
1> 'std::pair<_Ty1,_Ty2>' : Types with user defined constructors are not aggregate
1> with
1> [
1> _Ty1=const char *,
1> _Ty2=const char *
1> ]
1>main.cpp(257): error C2552: 'pairs' : non-aggregates cannot be initialized with initializer list
1> 'std::pair<_Ty1,_Ty2>' : Types with user defined constructors are not aggregate
1> with
1> [
1> _Ty1=const char *,
1> _Ty2=const char *
1> ]
Upvotes: 0
Views: 2539
Reputation: 303
On Ubuntu g++ 4.9.2 it works fine. Don`t know for sure about Visual Studio 2012. But first variant is that you need to use a constructor, and a second this feature is added in VS2013(bracket initialization).
Upvotes: 0
Reputation: 409196
What you are doing is uniform initialization which was introduced in C++11. The C++11 compatibility in VS2012 is minimal at best, and missing completely in many areas.
You simply can't use a syntax like that using that version of Visual Studio. Instead you have to use e.g. std::make_pair
:
const std::pair<const char*, const char*> pairs[] = {
std::make_pair("String A", "String 1"),
std::make_pair("String B", "String 2")
};
Upvotes: 1