第一篇文章,所以请放轻松我:)。我不认为我真的需要为此提出任何实际代码,但如果我错了,请告诉我。这是我大学编程课上的家庭作业。我对如何正确使用我的#include语句感到困惑。这是我的文件结构:
基本上,我的第一直觉是" #include" header.h" "在room.h,ship.h,main.cpp和functions.cpp中。那么" #include" ship.h"在ship.cpp和" #include room.h"在room.cpp。然而,我开始在wazoo上犯错误。我在课堂上遇到了类似的问题,但我让我的老师在那里进行整理,我不确定我们是如何做到的,而且我也知道大量的错误通常表明包含错误。
它令人烦恼,因为我在添加functions.cpp之前以某种方式工作,但我真的想保持main.cpp很干净,所以我宁愿将函数放在一个单独的文件中。
在这种情况下,包含的最佳模式是什么?
编辑:我发布我的3个头文件
header.h
/*
Author: *********
Class : **********
Assignment : Programming Assignment 2
Description :
This program will construct a ship for the user. It accepts input from a file
containing information on various rooms. It will then check the rooms
validity and add it to the ship if it's valid. Once all of the rooms have been added,
the program will determine if the entire ship is valid and let the user know.
Certification of Authenticity :
I certify that this is entirely my own work, except where I have given
fully - documented references to the work of others.I understand the
definition and consequences of plagiarism and acknowledge that the assessor
of this assignment may, for the purpose of assessing this assignment :
-Reproduce this assignment and provide a copy to another member of
academic staff; and / or
- Communicate a copy of this assignment to a plagiarism checking
service(which may then retain a copy of this assignment on its
database for the purpose of future plagiarism checking)
*/
#ifndef header_h
#define header_h
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
#endif
room.h
#ifndef room_h
#define room_h
#include "header.h"
enum RoomType
{
UNKNOWN = -1,
BAY,
LATRINE,
CABIN,
BRIDGE,
NUM_ROOM_TYPES
};
const string ROOM_STRINGS[NUM_ROOM_TYPES] = { "Bay",
"Latrine",
"Cabin",
"Bridge"
};
class Room
{
public:
//default constructor
Room();
//constructor
Room( RoomType type, int width, int breadth, int height );
//destructor
~Room(){};
//accessors
inline RoomType getType() const { return mType; };
inline int getHeight() const { return mHeight; };
inline int getWidth() const { return mWidth; };
inline int getBreadth() const { return mBreadth; };
inline int getVolume() const { return getWidth() * getBreadth() * getHeight(); }; //currently unused
inline string getRoomName(){ return ROOM_STRINGS[mType]; };
string getDescription();
//mutators
void setType(RoomType type) {mType = type; };
void setHeight(int height) {mHeight = height; };
void setWidth(int width) {mWidth = width; };
void setBreadth(int breadth) {mBreadth = breadth; };
private:
//type of room
RoomType mType;
//floor dimensions - in feet
int mWidth;
int mBreadth;
//ceiling height - in feet
int mHeight;
};
#endif
ship.h
#ifndef ship_h
#define ship_h
#include "header.h"
const int MAX_BAY = 4;
const int MAX_LATRINE = 15;
const int MAX_BRIDGE = 1;
const int MAX_CABIN = 25;
const int MIN_BAY = 1;
const int MIN_LATRINE = 1;
const int MIN_BRIDGE = 1;
const int MIN_CABIN = 0;
const int MIN_ROOM_HEIGHT = 7;
const int MIN_ROOM_AREA = 20;
class Ship{
public:
Ship();
bool addRoom(const Room& theRoom);
string getDescription();
//Accessors
int getNumBays(){ return bayTotal; };
int getNumLatrines(){ return latrineTotal; };
int getNumBridges(){ return bridgeTotal; };
int getNumCabins(){ return cabinTotal; };
int getTotalSquareFootage(){ return totalSquareFootage; };
private:
Room Bay[MAX_BAY];
Room Latrine[MAX_LATRINE];
Room Bridge[MAX_BRIDGE];
Room Cabin[MAX_CABIN];
int bayTotal;
int latrineTotal;
int bridgeTotal;
int cabinTotal;
int totalSquareFootage;
bool isShipValid();
void addSquareFootage(float);
};
#endif
答案 0 :(得分:0)
出现什么样的错误?您的问题可能不止一次包含相同的标题。
尝试将此添加到每个标题:
#ifndef ROOM_H
#define ROOM_H
... code ...
#endif
要明确&#39; ROOM_H&#39;上面的每个标题都必须是唯一的。
答案 1 :(得分:0)
如果在不同的cpp文件中使用#include“room.h”,那么您可能会收到链接器错误,因为此处的下面不是类型声明。
const string ROOM_STRINGS[NUM_ROOM_TYPES] = { "Bay",
"Latrine",
"Cabin",
"Bridge"
};
您正在创建和分配名为ROOM_STRINGS的变量。通过在不同的cpp文件中声明它,您将拥有相同全局变量的多个副本,这是一个错误。您可以用
替换它static const string ROOM_STRINGS[NUM_ROOM_TYPES] = { "Bay",
"Latrine",
"Cabin",
"Bridge"
};
您仍然会有多个副本,但每个cpp文件都有自己的私有副本。更好的解决方案是将此声明与getRoomName的代码一起移动到room cpp文件中。
或者您可以在标题中将ROOM_STRINGS声明为extern,然后您仍然需要在cpp文件中添加变量分配。