简体   繁体   中英

Constructor error: error: expected ‘,’ or ‘…’ before numeric constant

I have the following code:

#include <memory>
#include <functional>
#include <string>
#include <unordered_map>

typedef std::shared_ptr<std::string> StrPtr;
auto hash1 = [](const StrPtr ptr) { return std::hash<std::string>()(*ptr); };

...

class Actor {

    ...

    private:
        std::unordered_multimap<StrPtr,StrPtr,decltype(hash1)> set(250000,hash1);
    ...
};

If I want to compile it I get the following error:

Actor.hpp:15:68: error: expected identifier before numeric constant
     std::unordered_multimap<StrPtr,StrPtr,decltype(hash1)> set(250000,hash1);
                                                                ^
Actor.hpp:15:68: error: expected ‘,’ or ‘...’ before numeric constant

Looking at the constructor definition of unordered_multimap, there seems to be no contradiction to my initialization. What is the problem here?

I compiled with gcc 4.8

Regular parentheses are not allowed in a brace-or-equals initializer. Use curly brackets:

std::unordered_multimap<StrPtr,StrPtr,decltype(hash1)> set{250000,hash1};

This limitation prevents ambiguity that would arise in some cases with function declarations, which use parenthesis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM