Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ won't let me use a struct as a template argument

Perhaps it is a header problem of sorts... But this is what's happening:

The compiler is giving me an error on the line:

Queue<Email> mailbox;

This is the error:

..\EmailSystem.h:25: error: ISO C++ forbids declaration of `Queue' with no type
..\EmailSystem.h:25: error: expected `;' before '<' token

Queue.h:

#ifndef QUEUE_H_
#define QUEUE_H_

#include <string>
#include "EmailSystem.h"

...

template <class B>
class Queue {
 ...
};

#endif /* QUEUE_H_ */

Queue.cpp:

#include "Queue.h"

...

template class Queue<Email>;

EmailSystem.h:

#ifndef EMAILSYSTEM_H_
#define EMAILSYSTEM_H_

#include <iostream>
#include <string>
#include <vector>
#include "Queue.h"

struct Email {
    ...
};

struct User {
    std::string name;
    Queue<Email> mailbox;
};

...

#endif /* EMAILSYSTEM_H_ */
like image 856
cactusbin Avatar asked Dec 13 '22 09:12

cactusbin


1 Answers

You have a circular include. Queue.h includes EmailSystem.h and EmailSystem.h includes Queue.h so the include guards make sure that the header has no effect the second time it is being included. This means if Queue.h is the first to be included then Queue will not yet be declared before it is first used in EmailSystem.h which it includes, at this point:

Queue<Email> mailbox;

I'm guessing, but I find it unlikely that your template Queue (if it really is a generic class template) needs to know about Email so you should probably remove #include "EmailSystem.h" from Queue.h to solve your issue.

like image 51
CB Bailey Avatar answered Dec 14 '22 22:12

CB Bailey