6

std::mapUbuntu 12.04 で clang-3.3 および clang-3.0を使用しようとすると問題が発生します。

#include <iostream>
#include <map>
#include <string>

class A
{
public:
#if 0 //clang compiles ok
    typedef std::map<std::string,std::string> MapKeyValue_t;
    void PrintMap(const MapKeyValue_t &my_map 
        = MapKeyValue_t())
#else // clang compiles fail
    void PrintMap(const std::map<std::string,std::string> &my_map 
    = std::map<std::string,std::string>())
#endif
{
    std::map<std::string,std::string>::const_iterator it;
    for (it = my_map.begin(); it != my_map.end(); it++)
    {
        std::cout << it->first << " " << it->second << std::endl;
    }
}
};

int main()
{
    A a;
    a.PrintMap();
    return 0;
}

ただし、コードは両方g++でコンパイルされclang、出力としてこれらのエラーが引き続き発生します。

test.cpp:14:36: error: expected ')'
        = std::map<std::string,std::string>())
                                          ^
test.cpp:13:15: note: to match this '('
        void PrintMap(const std::map<std::string,std::string> &my_map 
                     ^
test.cpp:14:24: error: expected '>'
        = std::map<std::string,std::string>())
                              ^
test.cpp:28:13: error: too few arguments to function call, expected 2, have 0
        a.PrintMap();
        ~~~~~~~~~~ ^
test.cpp:13:2: note: 'PrintMap' declared here
        void PrintMap(const std::map<std::string,std::string> &my_map 
        ^
3 errors generated.

私の問題に最も近いものは、次のトピックです: std::map をデフォルトのコンストラクターパラメーターとして渡す方法

しかし、何が悪いのかわかりません。うまくいけば、誰かがこれに光を当てることができます.

アップデート:

void PrintMap(const std::map<std::string,std::string> &my_map 
        = (std::map<std::string,std::string>()))

大丈夫です。ありがとう。

4

2 に答える 2

1

vs2012でコンパイルして実行しました。
だから私はそれがコンパイラの問題だと思います。

于 2013-08-10T10:00:17.413 に答える