[英]How do I split classes with inheritance up into different .h/.cpp files?
I have been working on a class and it is all big and ugly and would like to split each bit into its own cpp/.h files. 我一直在研究一个类,它很大而且很丑,想把每个比特分成它自己的cpp / .h文件。
I can not seam to work out how to do this... 我无法找到如何做到这一点......
Here is my code http://ideone.com/n2Vjb 这是我的代码http://ideone.com/n2Vjb
I understand that you place the class in the .h and the implementation in the .cpp... but I just can not get it to run, where it runs fine in a single file. 我知道你将类放在.h和.cpp中的实现中...但是我无法让它运行,它在单个文件中运行良好。
Make sure you don't forget to put: 确保你不要忘记放:
#include "HeaderFile.h"
in the cpp file, where "HeaderFile.h" is the name of the ".h" file containing the definition of the class you want to use in the cpp file. 在cpp文件中,“HeaderFile.h”是“.h”文件的名称,该文件包含要在cpp文件中使用的类的定义。
If you have a file "MapFrames.h" containing: 如果您有一个包含以下内容的文件“MapFrames.h”:
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
class MapFrames{
public:
char frame[11];
vector<short> cols;
vector<short> rows;
MapFrames (short a, short b);
};
then the cpp file "MapFrames.cpp" should contain: 那么cpp文件“MapFrames.cpp”应该包含:
#include "MapFrames.h"
MapFrames::MapFrames (short a, short b){
// Construct a vector "rows" of a certain user defined size
rows.resize(a);
// Construct a vector "cols" of a certain user defined size
cols.resize(b);
//Construct Frames
frame[0]=201; // Top LC
frame[1]=205; // Horizontal
frame[2]=187; // Top RC
frame[3]=186; // Vertical
frame[4]=200; // Bottom LC
frame[5]=188; // Bottom RC
//Construct Icons
frame[6]=219; // ICON: Hidden Locations
frame[7]=177; // ICON: Spy Location: Nothing Found
frame[8]=2; // ICON: Spy Location: City Found
frame[9]=127; // ICON: Miss
frame[10]=15; // ICON: Destroyed City
}
Gerald 杰拉尔德
The following compiles and runs for me (albeit with a number of compiler warnings which you should probably look into): 以下编译并运行(尽管有许多编译器警告你应该查看):
file.h: file.h:
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
class MapFrames{
public:
char frame[11];
vector<short> cols;
vector<short> rows;
MapFrames (short a, short b);
};
class Cities : public MapFrames{
public:
//Cords Holds map data. 0 = Unknown, 1 = City Location, 2 = Hit, 3 = Miss, 4 = Spy: Hit Nothing, 5 = Spy: Hit City
vector<short>P1cords;
vector<short>P2cords;
Cities (short a, short b);
};
class PrintFuncs : public Cities{
public:
PrintFuncs(short a, short b);
void PrintTop();
void PrintMid();
void PrintBot();
void PrintCords();
void PrintGrid();
};
class GameBoard : PrintFuncs{
public:
GameBoard (short a, short b);
void display();
};
#endif
file.cpp: file.cpp:
#include "file.h"
MapFrames::MapFrames (short a, short b){
// Construct a vector "rows" of a certain user defined size
rows.resize(a);
// Construct a vector "cols" of a certain user defined size
cols.resize(b);
//Construct Frames
frame[0]=201; // Top LC
frame[1]=205; // Horizontal
frame[2]=187; // Top RC
frame[3]=186; // Vertical
frame[4]=200; // Bottom LC
frame[5]=188; // Bottom RC
//Construct Icons
frame[6]=219; // ICON: Hidden Locations
frame[7]=177; // ICON: Spy Location: Nothing Found
frame[8]=2; // ICON: Spy Location: City Found
frame[9]=127; // ICON: Miss
frame[10]=15; // ICON: Destroyed City
}
Cities::Cities (short a, short b) : MapFrames (a,b){
//Player 1 (LEFT)
//Resize Vector to number of elements in map
P1cords.resize(a*b);
//set 1st 33% of elements (rounded up) to 1.
for (int i=0; i<ceil((33/100.f)*(a*b)); i++){
P1cords[i] = 1;
}
//Shuffle cords for random city locations.
random_shuffle ( P1cords.begin(), P1cords.end() );
//Player 2 (RIGHT)
//Resize Vector to number of elements in map
P2cords.resize(a*b);
//set 1st 33% of elements (rounded up) to 1.
for (int i=0; i<ceil((33/100.f)*(a*b)); i++){
P2cords[i] = 1;
}
//Shuffle cords for random city locations.
random_shuffle ( P2cords.begin(), P2cords.end() );
}
PrintFuncs::PrintFuncs(short a, short b) : Cities (a,b){
}
void PrintFuncs::PrintTop(){
cout<<frame[0]<<frame[1]<<frame[1]<<frame[1]<<frame[2];
}
void PrintFuncs::PrintMid(){
cout<<frame[3]<<" "<<frame[6]<<" "<<frame[3];
}
void PrintFuncs::PrintBot(){
cout<<frame[4]<<frame[1]<<frame[1]<<frame[1]<<frame[5];
}
void PrintFuncs::PrintCords(){
cout<<" ";
for (int i=0; i<cols.size(); ++i){
cout<<" "<<i<<" ";
}
cout<<" ";
for (int i=0; i<cols.size(); ++i){
if (i+cols.size()<10){
cout<<" "<<i+cols.size()<<" ";}
if (i+cols.size()>9){
cout<<" "<<i+cols.size()<<" ";}
}
cout<<"\n";
}
void PrintFuncs::PrintGrid(){
for (int j=0; j<rows.size(); ++j){
//Print TopRow
cout<<" ";
for (int i=0; i<cols.size(); ++i){
PrintTop();
}
cout<<" ";
for (int i=0; i<cols.size(); ++i){
PrintTop();
}
cout<<"\n";
//Print Middle Row
cout<<" "<<j<<" ";
for (int i=0; i<cols.size(); ++i){
PrintMid();
}
cout<<" ";
for (int i=0; i<cols.size(); ++i){
PrintMid();
}
cout<<"\n";
//Print Bottom Row
cout<<" ";
for (int i=0; i<cols.size(); ++i){
PrintBot();
}
cout<<" ";
for (int i=0; i<cols.size(); ++i){
PrintBot();
}
cout<<"\n";
}
}
GameBoard::GameBoard(short a, short b) : PrintFuncs (a,b){}
void GameBoard::display(){
PrintCords();
PrintGrid();
}
int main (){
short rows = 0;short cols = 0;
cout<<"Enter a value for ROWS - between 2-6: ";cin>>rows;
cout<<"Enter a value for COLS - between 2-7: ";cin>>cols;
cout<<endl<<endl;
// short rows = 5;short cols = 5;
GameBoard map(rows, cols);
map.display();
// End
std::cout<<std::endl<<std::endl<<std::endl<<"Please Close Console Window"<<std::endl;
std::cin.ignore('\n', 1024);
return(0);
}
In summary: includes, namespace use, and class declarations into the header file, and definitions in the source file. 总结:包含,命名空间使用和头文件中的类声明,以及源文件中的定义。 Include the header file at the top of the source file.
在源文件的顶部包含头文件。 The #ifndef, #define, and #endif ensure that the header file can only be included once, in case you need the declarations in other files.
#ifndef,#define和#endif确保头文件只能包含一次,以防您需要其他文件中的声明。 Also, as suggested by another answer, it would be neater to place each class into its own set of header/source files.
此外,正如另一个答案所建议的那样,将每个类放入其自己的头/源文件集中会更简洁。 This will have the added benefit that you won't need to worry about what order you list the class declarations: with the single-file approach, every class needs to be declared before it is used.
这将带来额外的好处,您不必担心列出类声明的顺序:使用单文件方法,每个类都需要在使用之前声明。 In your case, that means that each declaration for a base class needs to appear before the inheriting class declaration.
在您的情况下,这意味着基类的每个声明都需要出现在继承类声明之前。
Put "class" into .h, at best one per class (interface) 将“class”放入.h,每个类最多一个(接口)
class MapFrames{
public:
char frame[11];
vector<short> cols;
vector<short> rows;
MapFrames (short a, short b);
};
Put ...::... into .cpp (implementation) 将... :: ...放入.cpp(实现)
MapFrames::MapFrames (short a, short b){
// Construct a vector "rows" of
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.