免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5657 | 回复: 5
打印 上一主题 下一主题

c++的类模板中使用list的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-17 14:02 |只看该作者 |倒序浏览
哪位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++编译后,产生如下错误信息:
[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: 错误:expected `;' before ‘it’
item_40.cpp:36: 错误:‘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: 错误:依赖名‘std::list::iterator’被解析为非类型,但实例化却产生了一个类型
item_40.cpp:35: 附注:如果您想指定类型,请使用‘typename std::list::iterator’
[lej@localhost test_cpp]$

我把list<T>::iterator 中的T换成int之类的基本类型就可以了,list::iterator不能这样用吗?

论坛徽章:
0
2 [报告]
发表于 2009-07-17 14:41 |只看该作者
请使用‘typename std::list::iterator’

论坛徽章:
0
3 [报告]
发表于 2009-07-17 15:12 |只看该作者
应该不是那个问题导致的

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
4 [报告]
发表于 2009-07-17 15:19 |只看该作者

回复 #3 xdsupermanli 的帖子

都说的这么清楚了…………

原帖由 emacsnw 于 2009-7-17 14:41 发表
请使用‘typename std::list::iterator’

原帖由 xdsupermanli 于 2009-7-17 14:02 发表
item_40.cpp:35: 附注:如果您想指定类型,请使用‘typename std::list::iterator’
[lej@localhost test_cpp]$

论坛徽章:
0
5 [报告]
发表于 2009-07-17 16:00 |只看该作者
哦,知道了!在list<T>::iterator it = find(rep.begin(), rep.end(), item);前加个typename,把list<T>::iterator指定为一个type就好了。

唉!看来对C++还是太生疏了,以后要好好study了。多谢OwnWaterloo 和emacsnw

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
6 [报告]
发表于 2009-07-17 16:05 |只看该作者

回复 #5 xdsupermanli 的帖子

不用觉得自己生疏~~~

这问题……  也可以说是C++的错……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP