Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 超载';欧佩特<<';对于C+中带有模板类的容器类型+;_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 超载';欧佩特<<';对于C+中带有模板类的容器类型+;

C++ 超载';欧佩特<<';对于C+中带有模板类的容器类型+;,c++,templates,operator-overloading,C++,Templates,Operator Overloading,我想做的是: template <class T> ostream& operator<<(ostream& ou, map<T,T> x) { for(typename map<T,T>::iterator it = x.begin(); it != x.end(); it++) { ou << it->first << ": " << it->second

我想做的是:

template <class T>
ostream& operator<<(ostream& ou, map<T,T> x) {
    for(typename map<T,T>::iterator it = x.begin(); it != x.end(); it++) {
        ou << it->first << ": " << it->second << endl;
    }
    return ou;
}
模板

ostream&operator您的操作符仅适用于键类型和映射类型相同的映射(例如
std::map
std::map

您需要两个模板参数:

template <class K, class V>
std::ostream& operator<<(std::ostream& ou, const map<K,V>& x) { .... }
模板

std::ostream&operator您的操作符仅适用于键类型和映射类型相同的映射(例如
std::map
std::map

您需要两个模板参数:

template <class K, class V>
std::ostream& operator<<(std::ostream& ou, const map<K,V>& x) { .... }
模板

std::ostream&operator您的模板参数表示映射中的键和值相同。请尝试更改此选项:

template <class T>
模板
为此:

template <typename T1, typename T2>
模板

然后用这些新的T1和T2更新其余代码。

您的templat参数表示映射中的键和值相同。请尝试更改:

template <class T>
模板
为此:

template <typename T1, typename T2>
模板

然后用这些新的T1和T2更新剩下的代码。

您已经为
T类
创建了一个模板,但它应该用于两个类:

template <class T, class U>
ostream& operator<<(ostream& ou, map<T,U> x) {
    for(typename map<T,U>::iterator it = x.begin(); it != x.end(); it++) {
        ou << it->first << ": " << it->second << endl;
    }
    return ou;
}
模板

ostream&operator您已经为
T类
制作了一个模板,但它应该用于两个类:

template <class T, class U>
ostream& operator<<(ostream& ou, map<T,U> x) {
    for(typename map<T,U>::iterator it = x.begin(); it != x.end(); it++) {
        ou << it->first << ": " << it->second << endl;
    }
    return ou;
}
模板

ostream&operator您为使用相同类型的键和值的映射定义了一个模板,但您尝试输出的映射的键和值有两种不同的类型您为使用相同类型的键和值的映射定义了一个模板,而您尝试输出的映射的键和值有两种不同的类型