-2

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

我的错误代码:

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
4

1 回答 1

5

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

例如,在JokeServer.h

#ifndef JOKESERVER_H
#define JOKESERVER_H

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

// ADD THIS
class AdminServer;

另请注意,通过包含(例如,我怀疑, )间接包含的所有文件都必须包含才能访问类的定义AdminServer.hJokeServer.hJokeServer.cppAdminServer.hAdminServer

于 2013-01-20T19:05:37.103 回答