我有这个类(hashMap.h):
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
using std::cout;
using std::vector;
using std::endl;
using std::string;
class hashMap
{
public:
explicit hashMap(int hashEntrySize = 101) : hashVector(nextPrime(2 * hashEntrySize)), currentSize{ 0 }
{}
bool containsKey(const string & searchKey);
bool containsVector(const vector<string> searchVector);
void insert(const string & keyTarget, const vector<string> & insertVector);
void insertAfterReHash(const string & keyTarget, const vector<string> & insertVector);
int getCurrentSize() const;
void assignKey(string & newKey);
private:
enum EntryType { ACTIVE, EMPTY, DELETED };
struct hashEntry
{
vector<string> vectorValue;
EntryType status;
int keyID;
string key;
hashEntry(EntryType s = EMPTY)
:status(s), keyID{ -1 } {}
};
size_t hashFunction(const string & key);
bool isActive(int currentPos) const;
int findPos(const string & keyTarget);
void reHash();
vector<hashEntry> hashVector;
int currentSize;
};
一个函数头文件(functions.h):
#pragma once
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
{
hashMap hm(500);
//do stuff with object
return hm;
}
主文件:
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
#include "hashMap.h"
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
int main()
{
vector<string> words;
string line;
ifstream dictionaryFile;
dictionaryFile.open("largedictionary.txt");
words = readinWords(dictionaryFile);
dictionaryFile.close();
hashMap hm = computeAdjacentWords(words);
return 0;
}
我创建了hashMap类,我希望能够返回一个hashMap对象,但是这给了我一个&#34;错误C4430缺少类型说明符的错误 - 假设为int。&#34;我做错了什么?
答案 0 :(得分:1)
我把代码放在文件中,并且很好地要求编译器完成它的工作。这是列表中的第一个警告:
$ cc main.cpp -c
In file included from main.cpp:5:
In file included from ./hashMap.h:6:
./functions.h:16:1: error: unknown type name 'hashMap'
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
^
编译器不知道hashMap
是什么。当它到达带有错误的行时,尚未声明或定义hashMap
符号。
您不应该在头文件中定义函数。
将functions.h
重命名为functions.cpp
,在包含列表的末尾添加#include "functions.h"
。
创建一个新文件functions.h
,它只包含函数的声明(函数头)及其使用的类型:
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
#pragma once
//#include <iostream>
#include <vector>
#include <string>
//#include <fstream>
#include "hashMap.h"
using std::string;
using std::vector;
// Do you really need all these types here?
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words);
#endif // __FUNCTIONS_H__
答案 1 :(得分:0)
您在hashmap.h中包含functions.h BEFORE 定义了hashMap类。因此,当编译器读取functions.h时,未定义hashMap类。