Need guidance with template class and understanding line errors
and I am trying to now do a Breadth First Order for files but, when I implement this template class to use a Queue I get this errors?, any idea where this might be?, also I do not understand the way the line number where the error is, is specified (i.e myprogram.c:23:10) no idea where that is
program.c:11:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
program.c:22:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
program.c:33:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
program.c: In function ‘display_info’:
program.c:49:3: error: ‘q_type’ undeclared (first use in this function)
program.c:49:3: note: each undeclared identifier is reported only once for each function it appears in
program.c:49:10: error: ‘string’ undeclared (first use in this function)
program.c:49:18: error: ‘bfFilesQueue’ undeclared (first use in this function)
program.c:50:18: error: ‘bfDirsQueue’ undeclared (first use in this function)
Now the Queue class was taken from here, and I want to use it with strings , basically a file path
http://www.java2s.com/Code/Cpp/Generic/Createagenericqueue.htm
So my questions are, because i don't expect you to debug this for me.. but want to know the following:
I tried to declare the queue at the top after the #include's so that i have have access to them globally. It seems like it does not like it because i am trying to use it from the display_info function. How can I declare this so that I have access to those Queues from anywhere?
I dont understand how to check in which line it is telling about the error (i.e. :12:10),开发者_Go百科 i took that Template class from the link i posted.. not sure why would it throw an error.. How can I know the line number based on those weird numbers?
So the code and errors are just informational so that you may answer those two questions with enough information.. any help will be much appreciated.
Thank you
#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#define SIZE 100
template <class Qtype> class q_type {
Qtype queue[SIZE];
int head, tail;
public:
q_type() {
head = tail = 0;
}
void q(Qtype num);
Qtype deq();
};
template <class Qtype> void q_type<Qtype>::q(Qtype num)
{
if(tail+1==head || (tail+1==SIZE && !head)) {
cout << "Queue is full.\n";
return;
}
tail++;
if(tail==SIZE)
tail = 0; // cycle around
queue[tail] = num;
}
template <class Qtype> Qtype q_type<Qtype>::deq()
{
if(head == tail) {
cout << "Queue is empty.\n";
return 0;
}
head++;
if(head==SIZE)
head = 0;
return queue[head];
}
q_type<string> bfFilesQueue;
q_type<string> bfDirsQueue;
static int display_info(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
//Check the type of Flag (i.e File or Directory)
switch(tflag)
{
case FTW_D:
case FTW_F:
bfFilesQueue.q(fpath);
break;
case FTW_DP:
bfDirsQueue.q(fpath);
break;
}
return 0; /* Continue */
}
program.c:11:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘<’ token
That sounds like you're compiling the C++ source code as C.
A simple fix can then be to rename the file as program.cpp
.
Cheers & hth.,
You declare
q_type<string> bfFilesQueue;
q_type<string> bfDirsQueue;
before you actually declare q_type. Try putting these lines after the class
精彩评论