简体   繁体   English

错误:地图中的operator =含糊不清的重载 <int, std::basic_string<char> &gt; :: Elem :: t3 = 0

[英]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.

相关问题 C++“错误:传递&#39;const std::map <int, std::basic_string<char> &gt;&#39; 作为 &#39;this&#39; 的论点...&quot; - C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..." 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::basic_string(int&amp;)&#39; - error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’ 错误:“ operator ==”不匹配(操作数类型为“ Seat”和“ std :: string {aka std :: basic_string <char> }&#39;) - error: no match for 'operator==' (operand types are 'Seat' and 'std::string {aka std::basic_string<char>}') 从std :: map转换 <std::basic_string<char> ,std :: pair - conversion from std::map<std::basic_string<char>,std::pair<int,int(*)(const std::vector::Mat ostream和运算符std :: basic_string <char, std::char_traits<char> ,std :: allocator <char> &gt;? - ostream and operator std::basic_string<char, std::char_traits<char>, std::allocator<char>>? 错误:没有匹配的函数调用&#39;std :: map <std::__cxx11::basic_string<char> - error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM