开发者

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

开发者 https://www.devze.com 2023-04-08 20:22 出处:网络
Perhaps it is a header problem of sorts... But this is what\'s happening: The compiler is giving me an error on the line:

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_ */


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.


You #include "EmailSystem.h" in Queue.h, before you declare class Queue. So when the compiler tries to figure out how to create struct User, it has no clue what's the Queue<Email> you're trying to use.

Note that EmailSystem.h and Queue.h include each other.

0

精彩评论

暂无评论...
验证码 换一张
取 消