Why do parentheses matter when initializing an empty vector?

I am having trouble understanding the error. I work with a direct map of vectors (using strings and storing row vectors):

typedef std::map<std::string, std::vector<std::string> > TRouteMarkets;

The following code (stripped down),

void CFoo::Bar(const char* route, const char* market)
{
    // ...

    TRouteMarkets::key_type key(route);
    TRouteMarkets::mapped_type mapped();
    TRouteMarkets::value_type pair(key, mapped);

    // ...
}

produces the following error:

"Foo.cc", line 518: Error: could not find a match for std :: pair <const std :: string, std :: vector <std :: string → :: pair (const std :: string, std :: vector <std :: string> ()) needed in CFoo :: Bar (const char *, const char *).

But removing ()from the displayed, i.e.

TRouteMarkets::mapped_type mapped;

corrects the error. What for? Is an mappedempty row vector anyway?

+5
source share
2 answers

This is actually a function declaration:

TRouteMarkets::mapped_type mapped();

mapped, TRouteMarkets::mapped_type.

+7

Most Vexing Parse.

TRouteMarkets::mapped_type mapped();

mapped, TRouteMarkets::mapped_type.

++ 11 , .

TRouteMarkets::mapped_type mapped{}; // Not a function declaration anymore
+5

All Articles