Fork me on GitHub

使用类模板时的一点困惑

 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,a 33 //奇怪的是逗号少输出了一个 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 }

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

posted @ 2014-08-11 15:48  闻波  阅读(293)  评论(1编辑  收藏  举报
友情链接: