C++ 如何将继承类拆分为不同的.h/.cpp文件?
我一直在制作一个类,它又大又丑,我想把每个部分都分割成自己的cpp/.h文件 我不知道该怎么做 这是我的密码 我知道您将类放在.h中,将实现放在.cpp中。。。但我就是不能让它运行,因为它可以在一个文件中正常运行。将“class”放入.h中,每个类(接口)最多一个 将“类”放入.h中,每个类(接口)最多一个C++ 如何将继承类拆分为不同的.h/.cpp文件?,c++,class,C++,Class,我一直在制作一个类,它又大又丑,我想把每个部分都分割成自己的cpp/.h文件 我不知道该怎么做 这是我的密码 我知道您将类放在.h中,将实现放在.cpp中。。。但我就是不能让它运行,因为它可以在一个文件中正常运行。将“class”放入.h中,每个类(接口)最多一个 将“类”放入.h中,每个类(接口)最多一个 确保您不会忘记放置: #include "HeaderFile.h" 在cpp文件中,其中“HeaderFile.h”是包含要在cpp文件中使用的类的定义的“.h”文件的名称 如果您有一
确保您不会忘记放置:
#include "HeaderFile.h"
在cpp文件中,其中“HeaderFile.h”是包含要在cpp文件中使用的类的定义的“.h”文件的名称
如果您有一个文件“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);
};
杰拉尔德确保你不会忘记放:
#include "HeaderFile.h"
在cpp文件中,其中“HeaderFile.h”是包含要在cpp文件中使用的类的定义的“.h”文件的名称
如果您有一个文件“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);
};
Gerald以下是为我编译和运行的(尽管有一些编译器警告,您可能需要查看): 文件.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
\ifndef文件
#定义文件
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类映射框{
公众:
字符帧[11];
向量cols;
向量行;
图幅(短a、短b);
};
类别城市:公共地图框架{
公众:
//跳线保存地图数据。0=未知,1=城市位置,2=命中,3=未命中,4=间谍:不命中,5=间谍:命中城市
矢量线;
矢量线;
城市(短a、短b);
};
类别:公共城市{
公众:
PrintFuncs(短a、短b);
void PrintTop();
void PrintMid();
void PrintBot();
无效打印线();
void PrintGrid();
};
类游戏板:PrintFuncs{
公众:
游戏板(短a、短b);
void display();
};
#恩迪夫
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);
}
#包括“file.h”
MapFrames::MapFrames(短a、短b){
//构造具有特定用户定义大小的向量“行”
行。调整大小(a);
//构造具有特定用户定义大小的向量“cols”
cols.resize(b);
//构造框架
帧[0]=201;//顶部LC
帧[1]=205;//水平
帧[2]=187;//顶部RC
帧[3]=186;//垂直
帧[4]=200;//底部LC
帧[5]=188;//底部RC
//构造图标
帧[6]=219;//图标:隐藏位置
帧[7]=177;//图标:间谍位置:未找到任何内容
帧[8]=2;//图标:间谍位置:找到城市
帧[9]=127;//图标:未命中
帧[10]=15;//图标:被摧毁的城市
}
城市::城市(短a,短b):地图框(a,b){
//球员1(左)
//将矢量大小调整为地图中的元素数
p1.调整大小(a*b);
//将前33%的元素(向上舍入)设置为1。
对于(int i=0;i,以下为我编译和运行(尽管您可能需要查看一些编译器警告):
文件.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
\ifndef文件
#定义文件
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类映射框{
公众:
字符帧[11];
向量cols;
向量行;
图幅(短a、短b);
};
类别城市:公共地图框架{
公众:
//跳线保存地图数据。0=未知,1=城市位置,2=命中,3=未命中,4=间谍:不命中,5=间谍:命中城市
矢量线;
矢量线;
城市(短a、短b);
};
类别:公共城市{
公众:
PrintFuncs(短a、短b);
void PrintTop();
void PrintMid();
void PrintBot();
无效打印线();
void PrintGrid();
};
类游戏板:PrintFuncs{
公众:
游戏板(短a、短b);
void display();
};
#恩迪夫
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);
}
#包括“file.h”
MapFrames::MapFrames(短a、短b){
//构造具有特定用户定义大小的向量“行”
行。调整大小(a);
//构造具有特定用户定义大小的向量“cols”
cols.resize(b);
//构造框架
帧[0]=201;//顶部LC
帧[1]=205;//水平
帧[2]=187;//顶部RC
帧[3]=186;//垂直
帧[4]=200;//底部LC
帧[5]=188;//底部RC
//构造图标
帧[6]=219;//图标:隐藏位置
帧[7]=177;//图标:间谍位置:未找到任何内容
帧[8]=2;//图标:间谍位置:找到城市
帧[9]=127;//图标:未命中
帧[10]=15;//图标:被摧毁的城市
}
城市::城市(短a,短b):地图框(a,b){
//球员1(左)
//将矢量大小调整为地图中的元素数
p1.调整大小(a*b);
//将前33%的元素(向上舍入)设置为1。
对于(int i=0;i什么运行失败,或者您收到了什么错误消息?您是否可以将尝试将其放置在单独的文件中,以便我们可以将其与您的单个文件进行比较,并查看您的错误所在?什么运行失败,或者您收到了什么错误消息?您是否可以将尝试将其放置在单独的文件中,以便我们可以将其与您的si进行比较我把大部分的东西放在CPP文件里,只把类减速放在CPP文件里。谢谢大家!好的,我明白了…谢谢大家。我把大部分的东西放在CPP文件里,只把类减速放在CPP文件里。谢谢大家谢谢,伙计,我现在知道是谁干的了。我做错了。谢谢你的帮助。我只能把1分为正确的,而另一个人是第一个。仍然感谢你的努力。谢谢,伙计,我现在知道是谁干的了。我做错了。谢谢你的帮助。我只能把1分为正确的