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

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

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

c++的類模板中使用list的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-07-17 14:02 |只看該作者 |倒序?yàn)g覽
哪位XDJM幫我看看下面這段C++代碼為什么編譯不過去:
#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

template<class T>
class myset
{
        public:
                bool member(const T &item) const;
                void insert(const T &item);
                void remove(const T &item);
                int cardinality() const;
       
        private:
                list<T> rep;
};

template<class T>
bool myset<T>::member(const T &item) const
{
        return find(rep.begin(), rep.end(), item) != rep.end();
}

template<class T>
void myset<T>::insert(const T &item)
{
        if (!member(item)) rep.push_back(item);
}

template<class T>
void myset<T>::remove(const T &item)
{
        list<T>::iterator it = find(rep.begin(), rep.end(), item);
        if (it != rep.end()) rep.erase(it);
}

template<class T>
int myset<T>::cardinality() const
{
        return rep.size();
}

int main()
{
        myset<int>  int_set;

        int_set.insert(1);
        int_set.insert(2);
        int_set.insert(3);
        int_set.insert(4);

        if (int_set.member(2))
                printf("2 is the member of int_set\n");
        else
                printf("2 is not the member of int_set\n");

        printf("the cardinality of int_set is  %d\n", int_set.cardinality());

        int_set.remove(3);
        printf("the cardinality of int_set is  %d\n", int_set.cardinality());

        return 0;
}

上面這段代碼使用g++編譯后,產(chǎn)生如下錯(cuò)誤信息:
[lej@localhost test_cpp]$ g++ item_40.cpp -o item_40
item_40.cpp: In member function ‘void myset<T>::remove(const T&)’:
item_40.cpp:35: 錯(cuò)誤:expected `;' before ‘it’
item_40.cpp:36: 錯(cuò)誤:‘it’在此作用域中尚未聲明
item_40.cpp: In member function ‘void myset<T>::remove(const T&) [with T = int]’:
item_40.cpp:61:   instantiated from here
item_40.cpp:35: 錯(cuò)誤:依賴名‘std::list::iterator’被解析為非類型,但實(shí)例化卻產(chǎn)生了一個(gè)類型
item_40.cpp:35: 附注:如果您想指定類型,請(qǐng)使用‘typename std::list::iterator’
[lej@localhost test_cpp]$

我把list<T>::iterator 中的T換成int之類的基本類型就可以了,list::iterator不能這樣用嗎?

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2009-07-17 14:41 |只看該作者
請(qǐng)使用‘typename std::list::iterator’

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2009-07-17 15:12 |只看該作者
應(yīng)該不是那個(gè)問題導(dǎo)致的

論壇徽章:
2
青銅圣斗士
日期:2015-11-26 06:15:59數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-07-24 06:20:00
4 [報(bào)告]
發(fā)表于 2009-07-17 15:19 |只看該作者

回復(fù) #3 xdsupermanli 的帖子

都說的這么清楚了…………

原帖由 emacsnw 于 2009-7-17 14:41 發(fā)表
請(qǐng)使用‘typename std::list::iterator’

原帖由 xdsupermanli 于 2009-7-17 14:02 發(fā)表
item_40.cpp:35: 附注:如果您想指定類型,請(qǐng)使用‘typename std::list::iterator’
[lej@localhost test_cpp]$

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2009-07-17 16:00 |只看該作者
哦,知道了!在list<T>::iterator it = find(rep.begin(), rep.end(), item);前加個(gè)typename,把list<T>::iterator指定為一個(gè)type就好了。

唉!看來對(duì)C++還是太生疏了,以后要好好study了。多謝OwnWaterloo 和emacsnw

論壇徽章:
2
青銅圣斗士
日期:2015-11-26 06:15:59數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-07-24 06:20:00
6 [報(bào)告]
發(fā)表于 2009-07-17 16:05 |只看該作者

回復(fù) #5 xdsupermanli 的帖子

不用覺得自己生疏~~~

這問題……  也可以說是C++的錯(cuò)……
您需要登錄后才可以回帖 登錄 | 注冊(cè)

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

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP