C ++ Makefile,类的重新定义

时间:2015-05-24 07:47:39

标签: c++ class inheritance makefile

所以我正在尝试为我的程序制作一个非常简单的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 ++编码的新手,所以即使你不能帮我解决我的问题,我们也会非常感谢任何提示或建议!

1 个答案:

答案 0 :(得分:0)

您的标题文件中可能缺少包含警示。

尝试添加

#pragma once

作为每个头文件的第一行

请注意,#pragma once是一个特定于编译器的扩展,但它得到了很好的支持,适用于所有主要和最小的c ++编译器