[英]error: ambiguous overload for operator= in Map<int, std::basic_string<char> >::Elem::t3 = 0
I am finishing up work on my second template class (coverting an older map class I made into a template) and I am having the damnedest time figuring out how to initialize null values for a my structure. 我正在完成第二个模板类的工作(涵盖了我制作成模板的较旧的地图类),并且我有最该死的时间来弄清楚如何为我的结构初始化空值。
I implemented a map using a 2-3-4 tree, where every node is actually a 4-node with the proper values set to NULL (eg a 3-node will have 2 key/value pairs and three children, the the third pair and the four child pointer are set to null. 我使用2-3-4树实现了一个映射,其中每个节点实际上是一个将适当值设置为NULL的4节点(例如,一个3节点将有2个键/值对和3个子级,第三个对并且四个子指针设置为null。
The issue I am having is that my template throws an error (copied directly from my terminal): 我遇到的问题是我的模板抛出错误(直接从终端复制):
mymap.cpp:271:3: error: ambiguous overload for âoperator=â in âtemp->Map<int, std::basic_string<char> >::Elem::t3 = 0â
The code I am using that is causing this issue is: 我正在使用的导致此问题的代码是:
//k3 is a KEY value and t3 is a TYPE value
temp -> k3 = NULL;
temp -> t3 = NULL;
Now, I know the issue is that the NULL keyword is normally reserved for pointers, and resolves to \\0 or 0 I believe, and KEY and TYPE could be literally anything, which might not support such values. 现在,我知道问题是NULL关键字通常为指针保留,并且我解析为\\ 0或0,并且KEY和TYPE可以是任何文字,可能不支持此类值。
My question is what is the proper way to set a null value in such a case? 我的问题是在这种情况下设置空值的正确方法是什么?
Thanks! 谢谢!
Often what you want is the value obtained by the default constructor. 通常,您想要的是默认构造函数获得的值。
#include <utility>
#include <iostream>
#include <string>
template <class A, class B>
void zero(std::pair<A, B>& p)
{
p.first = A();
p.second = B();
}
int
main()
{
std::pair<int, std::string> p { 10, "foo" };
std::cout << p.first << ' ' << p.second << '\n';
zero(p);
std::cout << p.first << ' ' << p.second << '\n';
return 0;
}
Although, if you are trying to be really general the requirement of a default constructor can be a problem. 虽然,如果您尝试变得通用,那么默认构造函数的要求可能会成为问题。 In that case you usually give the caller some way to specify the default value.
在这种情况下,通常可以给调用方一些方式来指定默认值。 Such as:
如:
template <class A, class B>
void zero(std::pair<A, B>& p, A a = A(), B b = B())
{
p.first = a;
p.second = b;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.