亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3018 | 回復: 5
打印 上一主題 下一主題

為什么不能用value_type&而必須用const value_type&或者value_type非引用? [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2007-05-09 16:52 |只看該作者 |倒序瀏覽
自己模仿的一個pair模板

問題:為什么pair1(first_type& f,second_type& s):first(f),second(s){}不正確

源代碼
#include "head.h"
#include <vector>

template <typename T1,typename T2>
class pair1
{
public:
        typedef T1 first_type;
        typedef T2 second_type;

        first_type first;
        second_type second;

        pair1():first(first_type()),second(second_type()){}

        //pair1(const first_type& f,const second_type& s):first(f),second(s){}    //OK
        //pair1(first_type f,second_type s):first(f),second(s){}  //OK
        pair1(first_type& f,second_type& s):first(f),second(s){}  //ERROR

        template<typename U1,typename U2>
        pair1(pair1<U1,U2> &apair):first(apair.first),second(apair.second){}


};

int main()
{
        pair1<int,int> pair_int(0,0);
        pair1<int,int> pair_int2(pair_int);

        cout<<pair_int.first<<endl;
        cout<<pair_int.second<<endl;

        cout<<pair_int2.first<<endl;
        cout<<pair_int2.second<<endl;

return 0;
}

[ 本帖最后由 山外山 于 2007-5-9 16:54 編輯 ]

論壇徽章:
0
2 [報告]
發(fā)表于 2007-05-09 16:55 |只看該作者
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

錯誤提示
g++     pair.cpp   -o pair
pair.cpp: In function `int main()':
pair.cpp:27: no matching function for call to `pair1<int, int>::pair1(int, int)
   '
pair.cpp:6: candidates are: pair1<int, int>::pair1(const pair1<int, int>&)
pair.cpp:17:                 pair1<T1, T2>::pair1(T1&, T2&) [with T1 = int, T2
   = int]
pair.cpp:14:                 pair1<T1, T2>::pair1() [with T1 = int, T2 = int]
make: *** [pair] Error 1
(3 of 9): no matching function for call to `pair1<int, int>::pair1(int, int)

[ 本帖最后由 山外山 于 2007-5-9 16:57 編輯 ]

論壇徽章:
0
3 [報告]
發(fā)表于 2007-05-09 17:03 |只看該作者
>>pair1<int,int> pair_int(0,0);

0是常量,而引用必須要可引用之值,但是對于常量而言只能用const引用了

論壇徽章:
0
4 [報告]
發(fā)表于 2007-05-09 17:11 |只看該作者
明白。THX

論壇徽章:
0
5 [報告]
發(fā)表于 2007-05-09 20:47 |只看該作者
原帖由 山外山 于 2007-5-9 17:11 發(fā)表
明白。THX

沒明白,能否解釋下?
謝謝

論壇徽章:
0
6 [報告]
發(fā)表于 2007-05-09 22:52 |只看該作者
原帖由 怪怪虎 于 2007-5-9 20:47 發(fā)表

沒明白,能否解釋下?
謝謝


int main()
{
        int &i = 0;
        return 0;
}


這個編譯不過去,是一個道理。引用常量的時候,必須用const int &i=0 這種形式
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP