'I need help printing a queue in a header.h file (c++)
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
Solution 1:[1]
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';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ramsay |
