Reputation: 60
The problem is I have a console platformer game(everything is written in one .cpp file). I have a buffer structure and my goal is to move definition of the structure to engine.h and definition of buffer's functions to engine.cpp.
engine.h file:
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 file:
#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;
}
Like here fill() take character object and structure character is defined in 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());
The problem is engine.cpp cannot see character structure defined in Main.cpp, so how can I resolve this? I was thinking about lambdas but I'm pretty noobie. If you need some more explanation or more code ask me :)
Upvotes: 1
Views: 1270
Reputation: 65
As for me, I generally have two files per class (one for the .h and one for the .cpp)
First, I noticed that there are many other classes not declared, other than character
:
platform
classenemy
classAnd a suggestion:
engine.h
and engine.cpp
file should be called buffer.h
and buffer.cpp
because inside it you defined a class called buffer
(or vice versa).
For the sake of example, I left it like this.Here is how i separated the classes:
character
class within the character.h
file and its implementation in character.cpp
enemy
form character
in enemy.h
and enemy.cpp
(because enemy
class uses many character
class methods )platform
in the character.h
file and the implementation in character.cpp
Here are the files contents:
#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
#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;
}
#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;
}
#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
#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;
}
#ifndef PLATFORM_H
#define PLATFORM_H
class platform
{
public:
platform();
virtual ~platform();
protected:
private:
};
#endif // PLATFORM_H
#include "platform.h"
platform::platform()
{
//ctor
}
platform::~platform()
{
//dtor
}
Upvotes: 3
Reputation: 31
It looks like the character struct isn't defined in engine.cpp. You are passing in the struct, but engine.cpp doesn't know how to handle it. You have the struct defined in main.cpp. Try putting the struct in a main.h file and then include that in both main.cpp and engine.cpp.
Upvotes: 0