Problems in my tetris clone

Started by
12 comments, last by Toadhead 18 years, 11 months ago
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)
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.
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??

Whoops I missed that conversion:

Fpointer = (FrameBase*)&LoadScreen
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 __LOADclass 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??
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???

Think about this, in this segment:
#ifndef __LOAD#define __LOADclass 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
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.
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...
It is perfectly safe to have a base class pointer to a derived class instance. It is called polymorphism.

This topic is closed to new replies.

Advertisement