-1

I need help printing a queue in a header.h file.

for example: this works just fine:

main.cpp

void showq(queue<int> gq)
{
    queue<int> g = gq;
    while (!g.empty()) {
        cout << '\t' << g.front();
        g.pop();
    }
    cout << '\n';
}


int main()
{

   queue<int> gquiz;
   gquiz.push(10);
   gquiz.push(20);
   gquiz.push(30);

   showq(gquiz);



    return 0;
}

Output: 10 20 30

but this does not

main.cpp

int main()
{

   queue<int> gquiz;
   gquiz.push(10);
   gquiz.push(20);
   gquiz.push(30);

   showq(gquiz);



    return 0;
}

header.h:

void showq(queue<int> gq)
{
    queue<int> g = gq;
    while (!g.empty()) {
        cout << '\t' << g.front();
        g.pop();
    }
    cout << '\n';
}

Output: error: variable or field showq declared void

  • You probably forget to `#include ` into your header file. Also, check to have the `#include "header.h"` in your main.cpp file. You may check [this answer](https://stackoverflow.com/questions/364209/variable-or-field-declared-void) for more information on the error message you get. – rawrex Apr 20 '22 at 05:13
  • Do you include the header in `main.cpp`? There's a lot missing that prevents diagnosing the problem. Show all the includes in each file. – Retired Ninja Apr 20 '22 at 05:13
  • Please show a [mre] and the full compiler output – Alan Birtles Apr 20 '22 at 06:30
  • yes i did included #include and my "header.h" – Cristian Acosta Apr 20 '22 at 15:28

1 Answers1

0

Have you forget to add #include "header.h" in main.cpp?

main.cpp:

#include "header.h"
int main()
{

   queue<int> gquiz;
   gquiz.push(10);
   gquiz.push(20);
   gquiz.push(30);

   showq(gquiz);



    return 0;
}

By the way, perhaps it's a better way to declare your showq function in header.h:

#include <queue>

void showq(queue<int> gq);

And then define it in header.cpp:

#include "header.h"

void showq(queue<int> gq)
{
    queue<int> g = gq;
    while (!g.empty()) {
        cout << '\t' << g.front();
        g.pop();
    }
    cout << '\n';
}
ramsay
  • 3,197
  • 2
  • 14
  • 13