C ++“没有命名类型” - 无法编译源文件

时间:2013-01-20 19:02:37

标签: c++ compilation header-files

我正在尝试编译我的代码而我无法弄清楚我做错了什么。 我相信这可能是我的编辑顺序,但我无法弄清楚我的错误。

我的错误代码:

g++ -w -c JokeServer.cpp
In file included from JokeServer.h:4:0,
                 from JokeServer.cpp:52:
AdminServer.h:9:2: error: ‘JokeServer’ does not name a type
make: *** [JokeServer.o] Error 1

谢谢。

JokeServer.h文件:

#ifndef JOKESERVER_H
#define JOKESERVER_H

#include "AdminServer.h"
#include "Subject.h"
#include "Observer.h"
#include "Worker.h"

class JokeServer: public Subject {
private:
    AdminServer* m_admin_server;

public:
     //public functions
};

#endif

AdminServer.h文件:

#ifndef ADMINSERVER_H
#define ADMINSERVER_H

#include "JokeServer.h"
#include "AdminWorker.h"

class AdminServer {
private:
    JokeServer* m_joke_server;

public:
  //public functions        
};

#endif

我的文件:

CPPFLAGS = g++ -w

all: JokeServer JokeClient AdminClient

#-------------------------------------
# JOKE SERVER
#-------------------------------------
JokeServer: JokeServer.o Worker.o JokeGenerator.o ProverbGenerator.o AdminServer.o AdminWorker.o
    $(CPPFLAGS) JokeServer.o Worker.o JokeGenerator.o ProverbGenerator.o AdminServer.o AdminWorker.o -o JokeServer -lpthread

JokeServer.o: JokeServer.cpp JokeServer.h Subject.h
    $(CPPFLAGS) -c JokeServer.cpp

Worker.o: Worker.cpp Worker.h Observer.h
    $(CPPFLAGS) -c Worker.cpp

JokeGenerator.o: JokeGenerator.cpp JokeGenerator.h
    $(CPPFLAGS) -c JokeGenerator.cpp

ProverbGenerator.o: ProverbGenerator.cpp ProverbGenerator.h
    $(CPPFLAGS) -c ProverbGenerator.cpp

AdminServer.o: AdminServer.cpp AdminServer.h
    $(CPPFLAGS) -c AdminServer.cpp

AdminWorker.o: AdminWorker.cpp AdminWorker.h
    $(CPPFLAGS) -c AdminWorker.cpp

1 个答案:

答案 0 :(得分:5)

您的.h文件之间有循环参考。文件AdminServer.hJokeServer.h互相包含。您应该使用前向声明来解决此问题。

例如,在JokeServer.h

#ifndef JOKESERVER_H
#define JOKESERVER_H

// REMOVE THIS
// #include "AdminServer.h"

// ADD THIS
class AdminServer;

另请注意,包含AdminServer.h(例如,我怀疑,JokeServer.h)的间接包括JokeServer.cpp的所有文件都必须包含AdminServer.h访问类AdminServer的定义