怎么解决一同出现的error C2255、2065、2990等错误

C++语言 码拜 8年前 (2016-09-16) 1110次浏览
具体代码如下:
//d_hash.h头文件
#ifndef HASH_CLASS
#define HASH_CLASS
#include <iostream>
#include <vector>
#include <list>
#include <utility>
#include “d_except.h”
using namespace std;
template <typename T, typename HashFunc>
class suhash   //书hash
{
public:
#include “d_hiter.h”
suhash(int nbuckets, const HashFunc& hfunc = HashFunc());
suhash(T *first, T *last, int nbuckets, const HashFunc& hfunc = HashFunc());
bool empty() const;
int size() const;
iterator find(const T& item);
const_iterator find(const T& item) const;
pair<iterator,bool> insert(const T& item);
int erase(const T& item);
void erase(iterator pos);
void erase(iterator first, iterator last);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
int numBuckets;
vector<list<T> > bucket;
HashFunc hf;
int hashtableSize;
};
template <typename T, typename HashFunc>
suhash<T, HashFunc>::suhash(int nbuckets, const HashFunc& hfunc):
numBuckets(nbuckets), bucket(nbuckets), hf(hfunc),
hashtableSize(0)
{}
// constructor. initialize table from pointer range [first, last)
template <typename T, typename HashFunc>
suhash<T, HashFunc>::suhash(T *first, T *last, int nbuckets, const HashFunc& hfunc):
numBuckets(nbuckets), bucket(nbuckets), hf(hfunc),
hashtableSize(0)
{
T *p = first;
while (p != last)
{
insert(*p);
p++;
}
}
template <typename T, typename HashFunc>
bool suhash<T, HashFunc>::empty() const
{
return hashtableSize == 0;
}
template <typename T, typename HashFunc>
int suhash<T, HashFunc>::size() const
{
return hashtableSize;
}
template <typename T, typename HashFunc>
typename suhash<T, HashFunc>::iterator suhash<T, HashFunc>::find(const T& item)
{

bucketIter = myBucket.begin();
while(bucketIter != myBucket.end())
{

if (*bucketIter == item)
return iterator(this, hashIndex, bucketIter);
bucketIter++;
}
// return iterator positioned at the end of the hash table
return end();
}
template <typename T, typename HashFunc>
typename suhash<T, HashFunc>::const_iterator
suhash<T, HashFunc>::find(const T& item) const
{

const list<T>& myBucket = bucket[hashIndex];
bucketIter = myBucket.begin();
while(bucketIter != myBucket.end())
{

if (*bucketIter == item)
return const_iterator(this, hashIndex, bucketIter);
bucketIter++;
}
return end();
}
template <typename T, typename HashFunc>
pair<typename suhash<T, HashFunc>::iterator,bool>
suhash<T, HashFunc>::insert(const T& item)
{

int hashIndex = int(hf(item) % numBuckets);
list<T>& myBucket = bucket[hashIndex];

list<T>::iterator bucketIter;
bool success;
bucketIter = myBucket.begin();
while (bucketIter != myBucket.end())
if (*bucketIter == item)
break;
else
bucketIter++;
if (bucketIter == myBucket.end())
{
bucketIter = myBucket.insert(bucketIter, item);
success = true;
hashtableSize++;
}
else
success = false;

return pair<iterator,bool>
(iterator(this, hashIndex, bucketIter), success);
}
template <typename T, typename HashFunc>
void suhash<T, HashFunc>::erase(iterator pos)
{
if (hashtableSize == 0)
throw underflowError(“hash erase(pos): hash table empty”);
if (pos.currentBucket == -1)
throw referenceError(“hash erase(pos): invalid iterator”);
bucket[pos.currentBucket].erase(pos.currentLoc);
}
template <typename T, typename HashFunc>
void suhash<T, HashFunc>::erase(typename suhash<T, HashFunc>::iterator first,
typename suhash<T,
HashFunc>::iterator last)
{
if (hashtableSize == 0)
throw underflowError(“hash erase(first,last): hash table empty”);
while (first != last)
erase(first++);
}
template <typename T, typename HashFunc>
int suhash<T, HashFunc>::erase(const T& item)
{
iterator iter;
int numberErased = 1;
iter = find(item);
if (iter != end())
erase(iter);
else
numberErased = 0;
return numberErased;
}
template <typename T, typename HashFunc>
typename suhash<T, HashFunc>::iterator suhash<T, HashFunc>::begin()
{
suhash<T, HashFunc>::iterator tmp;
tmp.hashTable = this;
tmp.currentBucket = -1;
tmp.findNext();
return tmp;
}
template <typename T, typename HashFunc>
typename suhash<T, HashFunc>::const_iterator suhash<T, HashFunc>::begin() const
{
suhash<T, HashFunc>::const_iterator tmp;
tmp.hashTable = this;
tmp.currentBucket = -1;
tmp.findNext();
return tmp;
}
template <typename T, typename HashFunc>
typename suhash<T, HashFunc>::iterator suhash<T, HashFunc>::end()
{
suhash<T, HashFunc>::iterator tmp;
tmp.hashTable = this;
tmp.currentBucket = -1;
return tmp;
}
template <typename T, typename HashFunc>
typename suhash<T, HashFunc>::const_iterator suhash<T, HashFunc>::end() const
{
suhash<T, HashFunc>::const_iterator tmp;
tmp.hashTable = this;
tmp.currentBucket = -1;
return tmp;
}
#endif   // HASH_CLASS
//d_except.h头文件
#ifndef EXCEPTION_CLASSES
#define EXCEPTION_CLASSES
#include <strstream>
#include <string>
using namespace std;
class baseException
{
public:
baseException(const string& str = “”):
msgString(str)
{
if (msgString == “”)
msgString = “Unspecified exception”;
}
string what() const
{
return msgString;
}
protected:
string msgString;
};
class memoryAllocationError: public baseException
{
public:
memoryAllocationError(const string& msg = “”):
baseException(msg)
{}
};
class rangeError: public baseException
{
public:
rangeError(const string& msg = “”):
baseException(msg)
{}
};
class indexRangeError: public baseException
{
public:
indexRangeError(const string& msg, int i, int size):
baseException()
{
char indexString[80];
ostrstream indexErr(indexString, 80);
indexErr << msg << ”  index ” << i << ”  size = ” << size << ends;
msgString = indexString;
}
};
class underflowError: public baseException
{
public:
underflowError(const string& msg = “”):
baseException(msg)
{}
};
class overflowError: public baseException
{
public:
overflowError(const string& msg = “”):
baseException(msg)
{}
};
class expressionError: public baseException
{
public:
expressionError(const string& msg = “”):
baseException(msg)
{}
};
class referenceError: public baseException
{
public:
referenceError(const string& msg = “”):
baseException(msg)
{}
};
class notImplementedError: public baseException
{
public:
notImplementedError(const string& msg = “”):
baseException(msg)
{}
};
class dateError: public baseException
{
public:
dateError(const string& first, int v, const string& last):
baseException()
{
char dateStr[80];
ostrstream dateErr(dateStr, 80);
dateErr << first << ” ” << v << ” ” << last << ends;
msgString = dateStr;
}
};
class graphError: public baseException
{
public:
graphError(const string& msg = “”):
baseException(msg)
{}
};
class fileOpenError: public baseException
{
public:
fileOpenError(const string& fname):
baseException()
{
char errorStr[80];
ostrstream fileErr(errorStr, 80);
fileErr << “Cannot open “” << fname << “”” << ends;
msgString = errorStr;
}
};
class fileError: public baseException
{
public:
fileError(const string& msg = “”):
baseException(msg)
{}
};
#endif // EXCEPTION_CLASSES
//cpp文件
#include<iostream>
#include<string>
#include”d_except.h”
#include”d_hash.h”
#include”d_hashf.h”
#include”d_hiter.h”
using namespace std;
class sHash
{
public:
unsigned int operator()(const string& s)const
{
unsigned int hashval;
if(s.length()==0)
hashval=0;
else
hashval=s[0]+s[s.length()-1];
return hashval;
}
};
int main()
{
suhash<string,sHash>ht(19);
string sarr[]={“house”,”dog”,”printer”,”animal”,”tea”,”key”,”icon”};
int arrint=sizeof(asrr)/sizeof(string);
ht::iterator hIter;
for(int i=0;i<arrint;i++)
ht.insert(sarr[i]);
for(hIter=ht.begin();hIter!=ht.end();hIter++)
cout<<*hIter<<” “<<endl;
return 0;
}
编译后出现以下错误:
1>e:\c++stl\no12\fuxi2\fuxi2\d_hashf.h(67): warning C4018: “<”: 有符号/无符号不匹配
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(9): error C2255: “friend”: 不允许位于类定义之外
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(12): error C2255: “friend”: 不允许位于类定义之外
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(20): error C2065: “T”: 未声明的标识符
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(20): error C2065: “HashFunc”: 未声明的标识符
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(20): error C2990: “suhash”: 非类 模板 已经声明为类 模板
1>          e:\c++stl\no12\fuxi2\fuxi2\d_hash.h(15) : 参见“suhash”的声明
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(55): error C2143: 语法错误 : 缺少“;”(在“&”的前面)
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(55): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(56): error C4430: 缺少类型说明符 – 假定为 int。注意: C++
不支持默认 int
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(102): error C2065: “T”: 未声明的标识符
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(102): error C2065: “HashFunc”: 未声明的标识符
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(107): error C2899: 不能在模板声明之外使用类型名称
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(107): error C2065: “T”: 未声明的标识符
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(107): error C2955: “std::list”: 使用类 模板 需要 模板
参数列表
1>          c:\program files\microsoft visual studio 10.0\vc\include\list(579) : 参见“std::list”的声明
1>e:\c++stl\no12\fuxi2\fuxi2\d_hiter.h(107): fatal error C1903: 无法从以前的错误中恢复;正在停止编译
因代码太多,部分代码放在本贴的回复中。本人已经查找了两天了,还是找不到原因,烦请各位大师指点。

解决方案

20

把 cpp 文件里的 #include”d_hiter.h” 删掉。
不过归根究底,就不应该让这个头文件出现,它应该是 d_hash.h 的一部分,没有单独出现的必要

10

这是你本人写的吗,友元和模板用的有问题啊

5

引用:

回复3楼:不是本人本人写的代码,全部的头文件都是从《数据结构 C++语言描述–应用标准模板库(STL)(第2版)》所附代码复制过来的,本人是在做这本中第12章的复习题。不好意思,问一下您可以不可以将所回复的再说具体些。谢谢。

如二楼所说的,你的d_hiter.h里面单独定义了iterator和const_iterator,这两个类应该属于su_hash的内容,第一个错误提示你freind的不能类外定义,所以d_hiter.h的代码必然在类内,结合模板的使用,应该把你iterator和const_iterator,这两个类放到你的hash类里面

5

cpp代码如下:
int main()
{
suhash<string,sHash>ht(19);
string sarr[]={“house”,”dog”,”printer”,”animal”,”tea”,”key”,”icon”};
int arrint=sizeof(sarr)/sizeof(string);
suhash<string,sHash>::iterator hIter;
for(int i=0;i<arrint;i++)
ht.insert(sarr[i]);
for(hIter=ht.begin();hIter!=ht.end();hIter++)
cout<<*hIter<<” “<<endl;
return 0;
}
运行结果:
key
tea
icon
dog
house
animal
printer

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么解决一同出现的error C2255、2065、2990等错误
喜欢 (0)
[1034331897@qq.com]
分享 (0)