免费注册 查看新帖 |

Chinaunix

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

[函数] 声明模板函数的时候出了问题(想了一晚上了) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-06 22:49 |只看该作者 |倒序浏览
编译文件的时候出现了以下的错误
HashTable.h: In member function ‘Element* CHashTable<Key, Element>::InsertElement(Key&, Element*)’:
HashTable.h:138: 错误:expected `;' before ‘res’
HashTable.h:139: 错误:‘res’ was not declared in this scope
......
......
HashTable.h: In member function ‘Element* CHashTable<Key, Element>::InsertEleme nt(Key&, Element*) [with Key = std::wstring, Element = CWord]’:
CDictionary.cpp:138:   instantiated from here
HashTable.h:138: 错误:dependent-name ‘std::vector<Element*,std::allocator<Elem ent*> >::iterator’ is parsed as a non-type, but instantiation yields a type
HashTable.h:138: 附注:如果您想指定类型,请使用 ‘typename std::vector<Element*, std::allocator<Element*> >::iterator’


原文件代码如下:
#include <iterator>
#include <vector>
#include <algorithm>
//#include "HashFunction.h"

#if !defined(AFX_HASHTABLE_H__90D3106E_BFA1_4A9B_A165_6B6D9849F8E2__INCLUDED_)
#define AFX_HASHTABLE_H__90D3106E_BFA1_4A9B_A165_6B6D9849F8E2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template <class Key, class Element>
class CHashTable
{
public:
        CHashTable();
        CHashTable(unsigned long ulBufSize);
        CHashTable(unsigned long ulBufSize,     unsigned long(*hashfun)(Key&, unsigned long), bool(*littlefun)( Element *,  Key *));

        virtual ~CHashTable();

        inline Element * InsertElement(Key& key, Element * pElement); //插入关键之为key的单元的指针pElement
.....
.....
}

template<class Key, class Element>
inline Element * CHashTable<Key, Element>::InsertElement(Key& key, Element * pElement)
{ //插入关键值为key的单元的指针pElement
        unsigned long temp = HashFunction(key, m_ulBufSize);
        std::vector<Element *>::iterator res;
        res=std::lower_bound(m_pData[temp].begin(), m_pData[temp].end(), &key, IsLittle);
        if( res == m_pData[temp].end())
        {
                m_pData[temp].push_back(pElement); m_ulEleCnt++;
        }
        else if( !IsEqual(*res, &key)) {
                m_pData[temp].insert(res, pElement); m_ulEleCnt++;
        }
        else  return *res;//如果键值已经存在则直接返回该单元的指针
        return 0;  //如果键值不存在则插入该单元,并返回0
}

论坛徽章:
0
2 [报告]
发表于 2006-03-06 22:50 |只看该作者
哪位大哥能帮忙检查一下啊
小弟感激不尽

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2006-03-06 22:57 |只看该作者
楼主能给一个全的,
可以编译调试的?

论坛徽章:
0
4 [报告]
发表于 2006-03-07 21:45 |只看该作者
class std::vector<Element *>::iterator res;



std::vector<Element *>::iterator res;

这两种定义方式不一样吗?为什么前者就可以编译通过呀?

论坛徽章:
0
5 [报告]
发表于 2006-03-07 21:48 |只看该作者
class std::vector<Element *>::iterator res;
汗~~这个是什么~~居然没看懂~

论坛徽章:
0
6 [报告]
发表于 2006-03-07 21:56 |只看该作者
哈哈
HashTable.h:138: 附注:如果您想指定类型,请使用 ‘typename std::vector<Element*, std::allocator<Element*> >::iterator
这一句里有typename,我就在前面加上class了
然后就不出编译错误了
但是编译出来的文件没有办法链接

论坛徽章:
0
7 [报告]
发表于 2006-03-07 22:24 |只看该作者
typename是为了方便从模块参数里引出型别而设的吧~~我还是想不通这个class加在这里什么意思?

论坛徽章:
0
8 [报告]
发表于 2006-03-08 12:22 |只看该作者
谢谢hellhell,我再看看吧

由于代码太多,以及别的一些原因,我没法上传代码
谢谢楼上的兄弟了

论坛徽章:
0
9 [报告]
发表于 2006-03-09 22:25 |只看该作者
由于vector<xxxx>::iterator 是vector定义的一个内嵌类型。
在模板中使用这种内嵌类型时,C++语法规定一定要使用typename在前面修饰,以明确告诉编译器,这是一个类型,而不要解析为成员变量

论坛徽章:
0
10 [报告]
发表于 2006-03-14 15:41 |只看该作者
感谢whiteear,正像你说的,在vector<xxxx>::iterator前面加上typename就可以解决我的问题

太感谢了!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP