C++ Makefile,class的重新定义
C++ Makefile, Redefinition of class
所以我正在尝试为我的程序制作一个非常简单的 makefile。该程序有 6 个 类 具有继承性,我为每个创建了单独的 .h 和 .cpp 文件。
我遇到的问题是,当我 运行 生成文件时,我收到一条错误消息:
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++ 编码的新手,所以即使您不能帮助我解决我的问题,任何提示或建议也将不胜感激!
您的头文件中可能缺少包含保护。
尝试添加
#pragma once
作为每个头文件的第一行
请注意,#pragma once 是编译器特定的扩展,但它得到了很好的支持,并且适用于所有主要和大多数次要的 c++ 编译器
所以我正在尝试为我的程序制作一个非常简单的 makefile。该程序有 6 个 类 具有继承性,我为每个创建了单独的 .h 和 .cpp 文件。
我遇到的问题是,当我 运行 生成文件时,我收到一条错误消息:
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++ 编码的新手,所以即使您不能帮助我解决我的问题,任何提示或建议也将不胜感激!
您的头文件中可能缺少包含保护。
尝试添加
#pragma once
作为每个头文件的第一行
请注意,#pragma once 是编译器特定的扩展,但它得到了很好的支持,并且适用于所有主要和大多数次要的 c++ 编译器