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++ Can';t使用重载运算符<&书信电报;要打印对象';s值_C++_Templates_C++11_Operator Overloading_Iostream - Fatal编程技术网

C++ Can';t使用重载运算符<&书信电报;要打印对象';s值

C++ Can';t使用重载运算符<&书信电报;要打印对象';s值,c++,templates,c++11,operator-overloading,iostream,C++,Templates,C++11,Operator Overloading,Iostream,我编写了元组实现,看起来很有效: template<typename T, typename... U> struct tuple{ T first; tuple<U...> second; tuple()=default; tuple(T t, U... u):first(t), second(u...){} std::ostream& print(std::ostream& stream){ st

我编写了元组实现,看起来很有效:

template<typename T, typename... U>
struct tuple{
    T first;
    tuple<U...> second;
    tuple()=default;
    tuple(T t, U... u):first(t), second(u...){}
    std::ostream& print(std::ostream& stream){
        stream<<first<<", ";
        return second.print(stream); //not using << to avoid extra () in output
    }
};

template<typename T>
struct tuple<T>{
    T first;
    tuple()=default;
    tuple(T t):first(t){}
    operator T&(){
        return first;
    }
    std::ostream& print(std::ostream& stream){
        return stream<<first;
    }
};

template<typename... T>
inline auto mk_tuple(T... t){
    return tuple<T...>(t...);
}
模板
结构元组{
T第一;
二元组;
tuple()=默认值;
元组(T,U…U):第一(T),第二(U…{}
标准::ostream和打印(标准::ostream和流){

stream签名错误,您需要:

template<typename... T>
std::ostream& operator<<(std::ostream &stream, const tuple<T...> &out){
//                                             ^^^^^
一般来说,谷歌“正确性”,并在C++中了解这个基本范式。
编辑:明白了!请尝试此签名:

template<typename T, typename... U>
std::ostream& operator<<(std::ostream &stream, const tuple<T, U...> &out){
    stream<<'(';
    return out.print(stream)<<")";
}
模板

标准::奥斯特雷姆和operator@GingerPlusPlus我看不懂你的心思,所以你必须更具体一点:在修复了操作符和
print
的两个签名之后,你到底得到了什么错误?
错误:无法绑定'std::ostream{aka std::basic_ostream}'左值到'std::basic_ostream&'
/usr/include/c++/4.8/ostream:602:5:错误:初始化'std::basic_ostream&std:'的参数1:operator@GingerPlusPlus哈。GCC 4.9和GCC 4.8在我的本地系统上都接受了这个代码。@GingerPlusPlus工作正常,我看不出代码有什么问题。奇怪。@hvd:I有g++4.8.2,Rextester上的是g++4.8.1,在这两种情况下都不起作用。。。
std::ostream& print(std::ostream& stream) const {
//                                        ^^^^^
template<typename T, typename... U>
std::ostream& operator<<(std::ostream &stream, const tuple<T, U...> &out){
    stream<<'(';
    return out.print(stream)<<")";
}