gpt4 book ai didi

c++ - 如何在 C++ 类函数中将 std::map 作为默认构造函数参数传递

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:45 29 4
gpt4 key购买 nike

尝试在 Ubuntu 12.04 上的 clang-3.3 和 clang-3.0 中使用 std::map 时遇到问题:

#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.

我能找到的最接近我的问题的是这个主题:How to pass std::map as a default constructor parameter

但是,我不知道出了什么问题。希望有人能对此有所说明。

更新:

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

没问题。谢谢。

最佳答案

我在vs2012编译运行成功。
所以我认为这是编译器的问题。

关于c++ - 如何在 C++ 类函数中将 std::map 作为默认构造函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160678/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com