我在一个头文件中定义了一个结构,其中包含另外三个#include
头文件的文件。一个是另一个头(queue.h(文件,它定义了一个非常基本的哈希表,另外两个是源代码,其中一个是从哈希表头(queue.cpp(定义函数,另一个包含main(p2.cpp(。
我遇到的问题是,该结构在p2.cpp中似乎工作正常,但在queue.h中,编译器告诉我该结构未定义。
这里是包含结构定义的p2.h。
#ifndef __P2_H__
#define __P2_H__
#define xCoor 0
#define yCoor 1
#include <iostream>
#include <string>
#include "queue.h"
#include "dlist.h" //linked list which I know works and is not the problem
using namespace std;
struct spot {
float key[2];
string name, category;
};
#endif /* __P2_H__ */
我在这个标题中包含了queue.h,所以我只需要在p2.cpp.中包含p2.h
这是p2.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "p2.h"
using namespace std;
int main () {
cout << fixed;
cout << setprecision (4);
Queue hashTable;
spot *spot1 = new spot;
spot1->key[xCoor] = 42.2893;
spot1->key[yCoor] = -83.7391;
spot1->name = "NorthsideGrill";
spot1->category = "restaurant";
hashTable.insert(spot1);
Dlist<spot> test = hashTable.find(42.2893, -83.7391);
while (!test.isEmpty()) {
spot *temp = test.removeFront();
cout << temp->key[xCoor] << " " << temp->key[yCoor] << " " << temp->name << " " << temp->category << endl;
delete temp;
}
return 0;
}
将和项放入哈希表中,然后将其取出。
这是队列.h
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <iostream>
#include <string>
#include "dlist.h"
#include "p2.h"
using namespace std;
class Queue {
// OVERVIEW: contains a dynamic array of spaces.
public:
// Operational methods
bool isEmpty();
// EFFECTS: returns true if list is empy, false otherwise
void insert(spot *o);
// MODIFIES this
// EFFECTS inserts o into the array
Dlist<spot> find(float X, float Y);
// Maintenance methods
Queue(); // ctor
~Queue(); // dtor
private:
// A private type
int numInserted;
int maxElts;
Dlist <spot>** queue;
// Utility methods
//Increases the size of the queue.
void makeLarger();
int hashFunc(float X, float Y, int modNum);
};
#endif /* __QUEUE_H__ */
这是queue.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "queue.h"
using namespace std;
bool Queue::isEmpty() {
return !numInserted;
}
void Queue::insert(spot *o) {
if (numInserted >= maxElts) {
makeLarger();
}
int index = hashFunc(o->key[xCoor], o->key[yCoor], maxElts);
queue[index] -> insertFront(o);
}
Queue::Queue() {
numInserted = 0;
maxElts = 1000;
queue = new Dlist<spot>*[maxElts];
for (int i = 0; i < maxElts; i++) {
queue[i] = new Dlist<spot>;
}
}
Queue::~Queue() {
for (int i = 0; i < maxElts; i++) {
delete queue[i];
}
delete[] queue;
}
void Queue::makeLarger() {
Dlist <spot>** temp = queue;
queue = new Dlist <spot>*[maxElts*2];
for (int i = 0; i < maxElts*2; i++) {
queue[i] = new Dlist<spot>;
}
for (int i = 0; i < maxElts; i++) {
while (!temp[i] -> isEmpty()) {
spot *spotTemp = temp[i] -> removeFront();
int index = hashFunc(spotTemp->key[xCoor], spotTemp->key[yCoor], maxElts*2);
queue[index] -> insertFront(spotTemp);
}
}
for (int i = 0; i < maxElts; i++) {
delete temp[i];
}
delete[] temp;
maxElts *= 2;
}
int Queue::hashFunc(float X, float Y, int modNum) {
return ((int)(10000*X) + (int)(10000*Y))%modNum;
}
Dlist<spot> Queue::find(float X, float Y) {
Dlist<spot> result;
Dlist<spot> *temp = new Dlist<spot>;
int index = hashFunc(X, Y, maxElts);
while (!queue[index] -> isEmpty()) {
spot *curSpot = queue[index] -> removeFront();
if ((curSpot->key[xCoor] == X) && (curSpot->key[yCoor] == Y)) {
result.insertFront(new spot(*curSpot));
}
temp -> insertFront(curSpot);
}
delete queue[index];
queue[index] = temp;
return result;
}
我相信问题出在我的queue.h文件中,因为它是我得到所有错误的地方,比如"spot尚未声明"。每次spot出现在队列中。h我至少有一个错误。我到处寻找类似的东西,但我能找到的只是有人试图在多个源文件中共享一个结构实例,或者把一个结构放在一个标头中并在多个来源文件中包含该标头的明显问题(这就是我正在做的,但我的问题似乎是一个相当独特的问题(。
您将queue.h
包含在实际定义spot
的标头中,因此到目前为止,文件实际包含的spot
尚未定义。
对于您的范围保护,请注意,以双下划线开头的标识符是由实现保留的,不要使用它们。
即使在简单的C:中,这也是一个糟糕的选择
#define xCoor 0
#define yCoor 1
改为使用这个:
enum {
xCoor = 0
, yCoor = 1
};
好的,首先永远不要在头文件中使用"using"子句(这会破坏名称空间的用途(
第二个提供一个完整的例子,无法编译
除了别人说的,你还有一个循环引用错误,这也可能导致类似的未定义符号错误。您有queue.h include p2.h,其中包含queue.h。