开发者

Struct defined in header and included in two source codes is only defined in one

I have a struct defined in a header file with three other files that #include that header file. One is another header(queue.h) file that defines a very basic hash table and the other two are source codes where one is defining the functions from the hash table header(queue.cpp) and the other contains main(p2.cpp).

The problem that I'm having is that the struct seems to work fine in p2.cpp but in queue.h the compiler is telling me that the struct is undefined.

Here is p2.h containing the struct definition.

#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 {
    fl开发者_运维问答oat key[2];
    string name, category;
};

#endif /* __P2_H__ */

I have queue.h included in this header so that I only have to include p2.h in p2.cpp.

Here is 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;
}

Places and item in the hash table and takes it back out.

Here is queue.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__ */

Here is 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;
}

I believe that the problem is in my queue.h file because it's where I get all of the errors like "spot has not been declared". Every time spot appears in queue.h I have at least one error. I searched around for anything like this but all I could find was people trying to share one instance of a struct across multiple source files, or the obvious question of putting a struct in a header and including that header across multiple source files(which is what I'm doing but my problem seems to be a rather unique one).


You are including queue.h within the header that actually defines spot, so by the point the file is actually included spot has not been defined yet.

For your scope guards, note that identifiers starting with a double underscore are reserved by the implementation, don't use them.

And this is a poor choice even in plain C:

#define xCoor 0
#define yCoor 1

use this instead:

enum {
    xCoor = 0
  , yCoor = 1
};


Ok first never ever using "using" clauses in header files (it destroys the purposes of namespaces)

2nd provide a complete example that fails to compile


In addition to what others have said, you also have a circular reference error, which can also lead to similar undefined symbol errors. You have queue.h include p2.h, which includes queue.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜