【发布时间】:2015-05-24 07:47:38
【问题描述】:
所以我正在尝试为我的程序制作一个非常简单的 makefile。该程序有 6 个继承类,我为每个类创建了单独的 .h 和 .cpp 文件。
我遇到的问题是,当我运行 makefile 时,我收到一条错误消息:
In file included from Items.h:2:0,
from GraphicsCards.h:3,
from GraphicsCards.cpp:1:
Orders.h:5:7: error: redefinition of ‘class Orders’
Orders.h:5:7: error: previous definition of ‘class Orders’
make: *** [GraphicsCards.o] Error 1
下面是我的 Makefile:
# all - compile the program if any source files have changed
all: Orders.o Customers.o Items.o GraphicsCards.o Proccessors.o HardDrives.o driver.o
g++ Orders.o Customers.o Items.o GraphicsCards.o Proccessors.o HardDrives.o driver.o -o program
# Orders.o
Orders.o: Orders.cpp Orders.h
g++ -c Orders.cpp -o Orders.o
# Customers.o
Customers.o: Customers.cpp Customers.h
g++ -c Customers.cpp -o Customers.o
# Items.o
Items.o: Items.cpp Items.h
g++ -c Items.cpp -o Items.o
# GraphicsCards.o
GraphicsCards.o: GraphicsCards.cpp GraphicsCards.h
g++ -c GraphicsCards.cpp -o GraphicsCards.o
# Proccessors.o
Proccessors.o: Proccessors.cpp Proccessors.h
g++ -c Proccessors.cpp -o Proccessors.o
# HardDrives.o
HardDrives.o: HardDrives.cpp HardDrives.h
g++ -c HardDrives.cpp -o HardDrives.o
# driver.o
driver.o: driver.cpp Orders.h Customers.h Items.h GraphicsCards.h Proccessors.h HardDrives.h
g++ -c driver.cpp -o driver.o
# clean - delete the compiled version of your program and
# any object files or other temporary files created during compilation.
clean:
rm -f *.o program
例如,这是我的 GraphicsCards.h 文件的样子:
#include <string>
#include "Orders.h"
#include "Items.h"
using namespace std;
class GraphicsCards: protected Items
{
private:
int speed;
string model;
int memory;
};
此外,我所有的 x.cpp 文件都将 #include "x.h" 作为第一行
我对 C++ 编码还很陌生,所以即使你不能帮助我解决我的问题,任何提示或建议都将不胜感激!
【问题讨论】:
-
您是否正确使用include guards?
标签: c++ class inheritance makefile