我有一个 Event 类和一个子类 ServerEvent,但是 Event 类只是 ServerEvent 扩展/继承的接口。当我运行 make 时,没有生成 Event.o 并且我收到一个错误,指出它不存在。
编译这个接口的正确方法是什么以及在makefile中添加什么?另外,我有一个受保护的构造函数的原因是 Event 不能被实例化。我不能使用虚拟构造函数——继承的正常方法是什么?
编辑:现在包括makefile、ServerEvent.cpp和编译错误
事件.h
#ifndef EVENT_H
#define EVENT_H
#include <string>
#define EVENT_STOP 0
#define EVENT_START 1
class Event {
private:
protected:
double time;
std::string label;
int type; // EVENT_START OR EVENT_STOP
Event();
public:
};
#endif
服务器事件.h
#ifndef SERVEREVENT_H
#define SERVEREVENT_H
#include "Event.h"
#include <vector>
class ServerEvent: public Event {
private:
public:
ServerEvent(std::vector<std::string> tokens);
};
#endif
服务器事件.cpp
#include "Event.h"
#include "ServerEvent.h"
#include <cstdlib>
#include <sstream>
ServerEvent::ServerEvent(std::vector<std::string> tokens) {
std::stringstream stream(tokens[0]);
stream >> time;
}
生成文件
OBJ = correngine.o CSVManager.o CorrelationEngineManager.o ServerEvent.o
CC = g++
CFLAGS = -c -Wall -pedantic
LFLAGS = -Wall -pedantic
EXE = correngine
correngine : $(OBJ)
$(CC) $(LFLAGS) $(OBJ) -o $(EXE)
correngine.o : correngine.cpp correngine.h CSVManager.h
$(CC) $(CFLAGS) correngine.cpp
CSVManager.o : CSVManager.cpp CSVManager.h
$(CC) $(CFLAGS) CSVManager.cpp
CorrelationEngineManager.o : CorrelationEngineManager.cpp CorrelationEngineManager.h Event.o
$(CC) $(CFLAGS) CorrelationEngineManager.cpp
Event.o : Event.h
$(CC) $(CFLAGS) Event.h
ServerEvent.o: ServerEvent.cpp ServerEvent.h Event.h
$(CC) $(CFLAGS) ServerEvent.cpp
clean :
\rm *.o $(EXE)
编译错误
ServerEvent.o: In function `ServerEvent::ServerEvent(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)':
ServerEvent.cpp:(.text+0x11): undefined reference to `Event::Event()'
ServerEvent.o: In function `ServerEvent::ServerEvent(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)':
ServerEvent.cpp:(.text+0xe1): undefined reference to `Event::Event()'
collect2: ld returned 1 exit status
make: *** [correngine] Error 1