1

私はクラスに取り組んできましたが、それはすべて大きくて醜く、各ビットを独自の cpp/.h ファイルに分割したいと考えています。

私はこれを行う方法を解決するために縫い合わせることができません...

ここに私のコードがあります http://ideone.com/n2Vjb

クラスを.hに配置し、実装を.cppに配置することは理解しています...しかし、単一のファイルで正常に実行される場合、実行できません。

4

3 に答える 3

2

次のことを忘れないようにしてください。

#include "HeaderFile.h"

ここで、「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);
};

その場合、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
}

ジェラルド

于 2012-04-13T13:03:01.117 に答える
2

以下は私のためにコンパイルして実行します(おそらく調べる必要がある多くのコンパイラ警告がありますが):

ファイル.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

ファイル.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);
}

要約すると、インクルード、ヘッダー ファイルへの名前空間とクラス宣言の使用、およびソース ファイルでの定義です。ソース ファイルの先頭にヘッダー ファイルをインクルードします。、#ifndef#defineおよび を#endif使用すると、ヘッダー ファイルを 1 回だけインクルードできるようになります。代わりに#pragma once、ほとんどのプリプロセッサで使用できます。また、別の回答で示唆されているように、各クラスを独自のヘッダー/ソース ファイルのセットに配置する方が適切です。これには、クラス宣言をリストする順序を気にする必要がないという追加の利点があります。単一ファイルのアプローチでは、すべてのクラスを使用する前に宣言する必要があります。あなたの場合、それは基本クラスの各宣言が継承クラス宣言の前に現れる必要があることを意味します。

于 2012-04-13T13:07:53.037 に答える
0

「クラス」を .h に入れます。クラス (インターフェース) ごとにせいぜい 1 つです。

class MapFrames{
public:
    char frame[11];
    vector<short> cols;
    vector<short> rows;
    MapFrames (short a, short b);
};

...::... を .cpp に入れる (実装)

MapFrames::MapFrames (short a, short b){
    // Construct a vector "rows" of 
...
于 2012-04-13T12:51:52.173 に答える