Jump to content
  • Advertisement
Sign in to follow this  

problems using polymorphism

This topic is 2331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using Visual C++ 2010, I am trying to develop the classic version of Tetris using SDL.

I am encountering an issue that I am not sure about in which section to post it but I hope I am posting this problem in the appropriate section.
Since, we all know that there are seven kinds of shapes in Tetris, theses are called O, I, T S, Z, J and L. All these shape perform the same functionalities such as move down, move right, move left and rotate and all these shapes rotate differently and collide differently than others. Only the O shape does not rotates. By using this idea, I am implementing the
shapes by using inheritance and polymorphism.

So, Here is the structure of the files in my project (As displayed in the solution explorer of the IDE)
Header Files:

Resource Files:

Source Files:

Right now, I have only implemented two shapes...
This is my main function...

#include "Game.h"
#include <iostream>
#undef main
int main()
Game game;
if ( !game.Execute() )
std::cout << "Failed to execute\n";
return 0;

this is the header file of the game class

#include <SDL.h>
#include "Event.h"

#include "Shape.h"
#include "Shape_I.h"
#include "Shape_O.h"

const int COL = 10;
const int ROW = 22; // Actually there are 18 rows, the top 4 are just to spawn the shapes and not to be rendered

class Game : public Event
Block Grid[ROW][COL]; // Tetris grid, two demensional representation

SDL_Surface* Display;

SDL_Surface* Background;

SDL_Surface* Block_Blue;

SDL_Surface* Block_Red;

SDL_Surface* Block_Lightblue;

SDL_Surface* Block_Orange;

SDL_Surface* Block_Yellow;

SDL_Surface* Block_Purple;

SDL_Surface* Block_Green;

bool Running;

Point Reference;

float timer;

Shape* shape;

void Reset();

SDL_Surface* getImage(int color);

bool Initialize(); // If initialized it returns True, else returns false

void OnEvent(SDL_Event* Event); // Handling Keyboard events

void Render(); // Drawing on Screen and displaying

void Cleanup(); // remove dynamic memories

void Update(); // Update game related variables

// Overloading inherited functions

void OnExit();

void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode);

Game(); // Constructor
//~Game(); // Destructor

bool Execute(); // Game execution loop

Here is the code of the Shape class that will be inherited by derived shape classes

#include "Block.h"

class Shape
Point* Position; // An Array that will contain coordinates of the blocks in that shape
int NoOfBlocks; // # of blocks in the shape which typically are always four

virtual ~Shape();

virtual bool MoveDown(Block (*Grid)[10], int ROW);

virtual void MoveRight(Block (*Grid)[10], int ROW, int COL);

virtual void MoveLeft(Block (*Grid)[10], int ROW, int COL);

virtual void Rotate(Block (*Grid)[10], int ROW, int COL);

Here is the header file for class "Block"

enum Color // Possible color of blocks
None, // Initializing value

struct Point
int X;
int Y;

class Block
int color;

Now, I am in the game execution loop, When I initialize the game, at the start I want to generate a random number ranged from 1 to 7 and based on that number I will assign the corresponding derived shape class object to the Shape pointer. If you notice the game.h file I posted, it has a private member "Shape* shape", this the pointer I will use to assign the different derived shapes so that it will simplify my implementation (using polymorphism)

Now that you have hopefully understood my situation, my problem is that whenever I compile this project I get errors that
error C2011: 'Color' : 'enum' type redefinition
error C2011: 'Point' : 'struct' type redefinition
error C2011: 'Block' : 'class' type redefinition
error C2011: 'Shape' : 'class' type redefinition

It is giving such errors because in the header files of the derived classes I have to include the header of Shape class so that I can use it for inheritance....

For example,

#include "Shape.h"

class Shape_O : public Shape
//........ etc....

The issue is that not only my derived shape classes will using this Shape class for inheritance but also the game class also has to include Shape class header file (Check game.h header file I posted above) along with other derived shape classes so that I can allocate new objects whenever they finish moving down...

This the problem I am having and I can't figure it out, I am an amateur programmer, I hope somebody can guide me here....

So in an nutshell, the enumeration, the struct and the Block class that I defined in "Block.h" header file is being redefined because two classes are using this header file and when I compile the program than it gives errors saying the I am redefining the same definitions....

Share this post

Link to post
Share on other sites

You're missing the include guard hoop that C++ makes you jump through.

Hey, you just solved my problem, I knew about these include guards before but never cared to take a good look at it....

Thank you......

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!