【问题标题】:how to split code into main.cpp header file如何将代码拆分为 main.cpp 头文件
【发布时间】:2021-04-16 10:31:04
【问题描述】:

问题是我有一个控制台平台游戏(一切都写在一个 .cpp 文件中)。我有一个缓冲区结构,我的目标是将结构的定义移动到 engine.h 并将缓冲区函数的定义移动到 engine.cpp。

engine.h 文件:

struct buffer
{
private:
    char** arr;
    char** dum;
    int width;
    int height;
    vector<platform> vc;
    vector<enemy*> ve;
public:
    buffer(const int, const int);
    void fill(character, vector<enemy*>);
    void render(const int, const int);
    char** getArray();
    void addPlatforms();
    void addE(enemy*);
    vector<platform> getPlatforms();
    vector<enemy*> getEnemies();
    ~buffer();
};

engine.cpp 文件:

#include "engine.h"
#include <fstream>
#include <vector>

void buffer::fill(chracter charact, vector<enemy*> ve)
{
    for (int i = 0; i < this->height; ++i)
    {
        for (int j = 0; j < this->width; ++j)
        {
            this->arr[i][j] = this->dum[i][j];
        }
    }
    this->arr[charact.getY()][charact.getX()] = '@';
    for (enemy* e : ve)
    {
        if (e->isAlive())
            this->arr[e->getY()][e->getX()] = 'M';
    }
    this->arr[0][max(charact.getX() - 10, 0)] = '0' + charact.getCoins() / 2;
}

像这里 fill() 获取字符对象和结构字符在 main.cpp 中定义。

struct character
{
private:
    int x;
    int y;
    int vx;
    int vy;
    bool onGround = false;
    bool up = false;
    bool left = false;
    bool right = false;
    bool jump = false;
    int coins = 0;
    int i = 0;
public:
    character(const int, const int);
    int getX();
    int getY();
    int getVy();
    int getCoins();
    void setRight(bool);
    void setLeft(bool);
    void setUp(bool);
    void setOnground(bool);
    int update(vector<platform>, vector<enemy*>);
    void collide(int vx, int vy, vector<platform>);
    bool collideO(platform);
};

int main()
{
    buffer screen{ 209, 15 };
    character charact{0, 12 };

    enemy en1{ 35, 12 };
    enemy en2{ 73, 12 };
    enemy en3{ 78, 8};
    enemy en4{ 51, 12 };

    screen.addE(&en1);
    screen.addE(&en2);
    screen.addE(&en3);
    screen.addE(&en4);

    screen.fill(charact, screen.getEnemies());

问题是engine.cpp 看不到Main.cpp 中定义的字符结构,我该如何解决呢?我在考虑 lambdas,但我很菜鸟。 如果您需要更多解释或更多代码,请询问我:)

【问题讨论】:

  • 也将字符移动到自己的头文件中。
  • 正如@ChrisMM 所说。或者您可以将其移至引擎标题。并且不要忘记在标题中使用 #pragma once 以避免以后出现循环包含。
  • 我做了但是engine.cpp仍然看不到字符

标签: c++


【解决方案1】:

就我而言,我通常每个班级有两个文件(一个用于 .h,一个用于 .cpp)

首先,我注意到除了character之外还有很多其他的类没有声明:

  • platform
  • enemy

还有一个建议:

  • 您的engine.h engine.cpp 文件应该称为buffer.hbuffer.cpp,因为在其中定义了一个名为buffer 的类(反之亦然)。 举个例子,我就这样离开了。

我是这样划分班级的:

  • 我在character.h 文件中定义了character 类,并在character.cpp 中实现了它
  • 我继承了enemy.henemy.cpp 中缺少的enemy 形式character(因为enemy 类使用了许多character 类方法)
  • 我在character.h 文件中定义了缺少的类platform,并在character.cpp 中定义了实现

以下是文件内容:

  • character.h
#ifndef CHARACTER_H
#define CHARACTER_H

#include "platform.h"

#include <vector>


class enemy;


class character
{
private:
    int x;
    int y;
    int vx;
    int vy;
    bool onGround = false;
    bool up = false;
    bool left = false;
    bool right = false;
    bool jump = false;
    int coins = 0;
    int i = 0;
public:
    character(const int, const int);
    int getX();
    int getY();
    int getVy();
    int getCoins();
    void setRight(bool);
    void setLeft(bool);
    void setUp(bool);
    void setOnground(bool);
    int update(std::vector<platform>, std::vector<enemy*>);
    void collide(int vx, int vy, std::vector<platform>);
    bool collideO(platform);
};

#endif // CHARACTER_H

  • character.cpp
#include "character.h"

character::character(const int, const int)
{
}

int character::getX()
{
    // only for compilation but you need to implement
    return 0;
}
int character::getY()
{
    // only for compilation but you need to implement
    return 0;
}
int character::getVy()
{
// only for compilation but you need to implement
    return 0;
}
int character::getCoins()
{
// only for compilation but you need to implement
    return 0;
}
void character::setRight(bool val)
{

}
void character::setLeft(bool val)
{

}
void character::setUp(bool val)
{

}
void character::setOnground(bool val)
{
}
int character::update(std::vector<platform>, std::vector<enemy*>)
{
// only for compilation but you need to implement
    return 0;
}
void character::collide(int vx, int vy, std::vector<platform>)
{
}
bool character::collideO(platform)
{
// only for compilation but you need to implement
    return false;
}

  • enemy.h

#ifndef ENEMY_H
#define ENEMY_H

#include "character.h"

#include <cmath>

class enemy:public character
{
    public:
        enemy(int,int);
        virtual ~enemy();
        bool isAlive();

    protected:

    private:
        int x, y;
};

#endif // ENEMY_H

-enemy.cpp


#include "enemy.h"

enemy::enemy(int a, int b):character(a,b)
{
    //ctor
}

enemy::~enemy()
{
    //dtor
}

bool enemy::isAlive()
{
    // only for compilation but you need to implement
    return false;
}


  • engine.h
#ifndef ENGINE_H
#define ENGINE_H

#include "platform.h"
#include "enemy.h"
#include "character.h"

#include <vector>

class buffer
{
private:
    char** arr;
    char** dum;
    int width;
    int height;
    std::vector<platform> vc;
    std::vector<enemy*> ve;
public:
    buffer(const int, const int);
    void fill(character, std::vector<enemy*>);
    void render(const int, const int);
    char** getArray();
    void addPlatforms();
    void addE(enemy*);
    std::vector<platform> getPlatforms();
    std::vector<enemy*> getEnemies();
    ~buffer();
};

#endif // ENGINE_H


  • engine.cpp
#include "engine.h"
#include <fstream>
#include <vector>
#include <cmath>

buffer::buffer(const int, const int)
{
}

buffer::~buffer() {}


void buffer::addE(enemy*)
{}

std::vector<enemy*> buffer::getEnemies()
{
    std::vector<enemy*> test;
    return test;
}



void buffer::fill(character charact, std::vector<enemy*> ve)
{
    for (int i = 0; i < this->height; ++i)
    {
        for (int j = 0; j < this->width; ++j)
        {
            this->arr[i][j] = this->dum[i][j];
        }
    }
    this->arr[charact.getY()][charact.getX()] = '@';
    for (enemy* e : ve)
    {
        if (e->isAlive())
            this->arr[e->getY()][e->getX()] = 'M';
    }
    this->arr[0][std::max(charact.getX() - 10, 0)] = '0' + charact.getCoins() / 2;
}



  • 平台.h
#ifndef PLATFORM_H
#define PLATFORM_H


class platform
{
    public:
        platform();
        virtual ~platform();

    protected:

    private:
};

#endif // PLATFORM_H

  • platform.cpp
#include "platform.h"

platform::platform()
{
    //ctor
}

platform::~platform()
{
    //dtor
}

【讨论】:

    【解决方案2】:

    看起来字符结构没有在engine.cpp 中定义。您正在传递结构,但 engine.cpp 不知道如何处理它。您在 main.cpp 中定义了结构。尝试将结构放在 main.h 文件中,然后将其包含在 main.cpp 和 engine.cpp 中。

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2017-11-24
      • 1970-01-01
      • 2020-01-30
      • 2014-03-12
      • 2021-07-26
      相关资源
      最近更新 更多