Error “xxxx”does not name a type

前端 未结 3 2147
花落未央
花落未央 2021-02-20 01:47

I encountered a problem when tried compiling the following code:

#include 
#include 
#include 
#include 

        
3条回答
  •  礼貌的吻别
    2021-02-20 02:25

    You cannot execute arbitrary expressions at global scope, so

    mapDial['A'] = 2;
    

    is illegal. If you have C++11, you can do

    map mapDial {
        { 'A', 2 }
    };
    

    But if you don't, you'll have to call an initialisation function from main to set it up the way you want it. You can also look into the constructor of map that takes an iterator, and use that with an array in a function to initialise the map, e.g.

    map initMap() {
        static std::pair data[] = {
            std::pair('A', 2)
        };
    
        return map(data, data + sizeof(data) / sizeof(*data));
    }
    
    map mapDial = initMap();
    

提交回复
热议问题