Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

Problems in my tetris clone

This topic is 4900 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

First a small question: Can I use classes without creating an object? I use a FrameBase abstract class and I have a few classes based on this BaseFrame class. Than I have a pointer that points to the BaseFrame class and I want to let this pointer point to the classes based on this FrameBase class. (LoadFrame is a class based on BaseFrame) So:

FrameBase *fpointer;
Fpointer = &LoadFrame; // Here the pointer will point to the loadframe class

Fpointer->Frame() //The Frame method is included in all classes based on BaseFrame






My problems: (from the debugger) - expected class-name before '{' token My class HAS a name! This is my class from the headerfile loading.h:
class LoadFrame : public FrameBase 
{
      
private:
      void Loop();
      int id;
         
              
protected:
                
                
public:
      //Public Methods
      int get_id();
      void Frame();

      //Constructor
      LoadFrame();      
      
};



My second problem: - expected primairy-expression before ';' token. (main.cpp line 64) This is my main.cpp:
/Including header files
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include "SDL_image.h"

//Including the main.h header files which includes the BaseFrame initialization
#include "main.h"


//Using the standard namespace
using namespace std;

//Static variables
SDL_Surface *screen; //The screen surface

FrameBase *Fpointer = NULL; //This pointer can point to all classes based on FrameBase





//Main starts here
int main(int argc, char** argv) 
{


Fpointer = &LoadFrame;

//Start General Gameloop
bool run = true;
while(run == true) 
{    
Fpointer->Frame();   
}
    
    
    

delete Fpointer;
//Game ends
 return 0;   
}




I hope someone can help me. Greetings, Rob (PS my game isn't finished but I decided to debug it now since there will probably be too many bugs when it's finished so I can better fix the bugs I have now so I have less bugs when the game is completed)

Share this post


Link to post
Share on other sites
Advertisement
Quickly glancing though, you will need to do something like (untested):

LoadFrame* MyFrame = new LoadFrame;
Fpointer = (FrameBase*)MyFrame;
...
delete Fpointer;

If you want to keep that delete there or

LoadFrame MyFrame;
Fpointer = &MyFrame;
...

If you do not want to delete a pointer.

In C++, you can't assign the address of the class/struct definition to a variable unless it is static or constant (and perhaps any other excpetions to the rule I've missed). So to answer your first question, no, not automatically, you must have a variable declared.

Share this post


Link to post
Share on other sites
After I removed the "delete Fpointer;", and used this:

LoadFrame LoadScreen;
Fpointer = &LoadScreen; (line 62)


I get this error:
"Cannot convert 'LoadFrame*' to 'FrameBase*' in assignment"
(main.cpp line 62)


And what about that strange class-name error??

Share this post


Link to post
Share on other sites
Cool!
Finaly it's fixed :)

Only 1 problem left:

"expected class-name before '{' token"
(loading.h line 5)

This is my full loading.h file:


#ifndef __LOAD
#define __LOAD

class LoadFrame : public FrameBase
{

private:
void Loop();
int id;


protected:


public:
//Public Methods
int get_id();
void Frame();

//Constructor
LoadFrame();

};

#endif



I hope you can fix this one too.
I don't understand it :(
Why does it say it's expecting a class name while I have a class name??

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Whoops I missed that conversion:

Fpointer = (FrameBase*)&LoadScreen;


Btw why this this need to be converted??
I tought a pointer to a base class could point to objects of classes based on that class too???

Share this post


Link to post
Share on other sites
Think about this, in this segment:

#ifndef __LOAD
#define __LOAD

class LoadFrame : public FrameBase
{

Where is the class FrameBase defined as? Remember that when you use include guards, you must re-include your class definitions that you use, so you will need to include the class definition for the missing class it cannot find.

You have to convert the pointer though, going from child to parent loses certain traits, so that's why a type cast is needed (which I used to very unsafe one [lol]) Take a look into static_cast at some safer options. There are other casting operators on that page as well. Theoretically the child is not the same as the parent, even though the child is derived from the parent, so you must cast that pointer to that of a parents. For more detailed info on that take a look int C++ Inheritance

Share this post


Link to post
Share on other sites
static_cast doesnt work, I needed to use reinterpret_cast (I know everything about typecasting).

But what I mean is that I tought a pointer to a parant class could point to child classes too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Toadhead
But what I mean is that I tought a pointer to a parant class could point to child classes too.


Ah, thanks about that reinterpret_cast, couldn't remember which one it was [wink]. Technically it can, which is why C++ is not a typesafe language, but that's something very bad to do! The child will have members and data that the praent might not, so if the variable that is a parent gets called with stuff from a child, who knows what will happen...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is perfectly safe to have a base class pointer to a derived class instance. It is called polymorphism.

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!