使用类模板时的一点困惑

来源:互联网 发布:51单片机综合设计 编辑:程序博客网 时间:2024/05/23 01:26
 1 #include<string> 2 #include<iostream> 3  4 using namespace std; 5  6 ///通过嵌套实现元则 7 template<typename T,typename N> 8 class my_tuple 9 {10 public:11     T value;12     N next;13     my_tuple(T const &v,N const &n):value(v),next(n){}14 };15 template<typename T,typename N>16 my_tuple<T,N> push(T const &v,N const &n)17 {18     return my_tuple<T,N>(v,n);19 }20 int main()21 {22     typedef my_tuple<int ,char> tuple2;23     typedef my_tuple<float,tuple2> tuple3;24     typedef my_tuple<std::string,tuple3> tuple4;25 26     tuple4 tup4 = push(std::string("awesome"),
27               push(.5f,
push(2,'a')));28 cout<<tup4.value<<","29 <<tup4.next.value<",";30 cout<<tup4.next.next.value<<","31 <<tup4.next.next.next<<endl;32 //此时输出结果为: awesome,0.52,a33 //奇怪的是逗号少输出了一个34 //开始我用的输出格式是cout<<tup4.value<<","<<tup4.next.value<","<<tup4.next.next.value<<","<<tup4.next.next.next<<endl;35 //直接就报错了:test.cpp||error: invalid operands of types 'const char [2]' and 'int' to binary 'operator<<'36 }

希望大神们看到了能给出一点提示

0 0
原创粉丝点击