Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:53:40+00:00 2026-05-26T02:53:40+00:00

I have a struct defined in a header file with three other files that

  • 0

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 {
    float 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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:53:41+00:00Added an answer on May 26, 2026 at 2:53 am

    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
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure defined in my header file: struct video { wchar_t* videoName;
I have an array of structs that I have defined in a header file:
I have a header file in which I have defined a struct. I want
I have a file Cache.cpp that has a corresponding header file Cache.h and another
I have a data structure defined as struct myDataStruct { int32_t header; int16_t data[8];
I have a structure Defined in the Header file for a class i am
I have a file named cpu.h that includes two other headers named register.h and
I have the following structure and object of structure defined in the header file
I have a structure defined in a header file called data.h. I am including
I want to have a function return a struct. So, in my header file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.