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

Perhaps this is a heading issue ... But here's what happens:

The compiler gives me an error in the line:

Queue<Email> mailbox; 

This is mistake:

 ..\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_ */ 
+4
source share
2 answers

You have a circular. Queue.h includes EmailSystem.h and EmailSystem.h includes Queue.h , so security guards must ensure that the header does not work the second time it is turned on. This means that if Queue.h is the first to be included, then Queue will not be announced until it is first used in EmailSystem.h , which it includes, at this point:

 Queue<Email> mailbox; 

I suppose, but it is unlikely that your Queue template (if it really is a general class template) should know about Email , so you should probably remove #include "EmailSystem.h" from Queue.h to solve your problem.

+11
source

You #include "EmailSystem.h" in Queue.h before you declare a class Queue . Therefore, when the compiler tries to figure out how to create a struct User , it does not know that you are trying to use Queue<Email> .

Please note that EmailSystem.h and Queue.h include each other.

+2
source

All Articles