Sign in to follow this  

Problems in my tetris clone

This topic is 4597 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
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
NOO!
Not a new problem :(

I placed this above the main function in the main.cpp file:

SDL_Surface *screen;

Doesn;t that mean it;s static?
And doesn't that mean I can use this in other source files too
Than why is this wrong in another source file?:

screen = SDL_SetVideoMode(500,400,32,SDL_HWSURFACE|SDL_DOUBLEBUF);



??

Share this post


Link to post
Share on other sites
I created a new SDL_Surface caled "loading_screen" in the loading.cpp file just to see if I get no further errors. (and used it like this: loading_screen = SDL_SetVideoMode(500,400,32,SDL_HWSURFACE|SDL_DOUBLEBUF); )

Now I get a linking error:

In function 'ZN9LoadFrame5FrameEv':
[linking error]undifined reference to 'LoadFrame::Loop()'
ID returned 1 exit status
[build error] [tetris.exe]Error 1


How am I going to fix this? :'(
(And still why doesn't my loading.cpp file recognize the screen surface while I created it above the main function?

Share this post


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


Ah crap, you are right [embarrass]. I don't know what I was thinking when I posted that. I guess I was meaning if you by chance, hackishly point one class to another, then *boom*. Brain lapse!

Quote:

SDL_Surface *screen;

Doesn;t that mean it;s static?
And doesn't that mean I can use this in other source files too
Than why is this wrong in another source file?:

screen = SDL_SetVideoMode(500,400,32,SDL_HWSURFACE|SDL_DOUBLEBUF);


Global to the scope of where it is defined in. If you want to use across many files, then you will have to make a extern declaration of it in your header file that you include in each .cpp file that want's to use it, i.e.:
extern SDL_Surface *screen; That way, the compiler knows it will be defined somewhere else, so it can be used as if it's declared in that scope of the file being compiled.

Also you do not have to do all of that. Rather than store the screen, you can always use SDL_GetVideoSurface() when you want a pointer to the screen. Makes life a lot easier [wink]. Just call : SDL_SetVideoMode(500,400,32,SDL_HWSURFACE|SDL_DOUBLEBUF); and you are all set!

Quote:

created a new SDL_Surface caled "loading_screen" in the loading.cpp file just to see if I get no further errors. (and used it like this: loading_screen = SDL_SetVideoMode(500,400,32,SDL_HWSURFACE|SDL_DOUBLEBUF); )

Now I get a linking error:

In function 'ZN9LoadFrame5FrameEv':
[linking error]undifined reference to 'LoadFrame::Loop()'
ID returned 1 exit status
[build error] [tetris.exe]Error 1


How am I going to fix this? :'(
(And still why doesn't my loading.cpp file recognize the screen surface while I created it above the main function?


Well the scren is because of the extern stuff, which I know you will fix now. The LoadFrame::Loop function is 1. not being defined or 2. The file it is defined in is not a part of the workspace. Check for both of those. Somewhere you will need to have:

void LoadFrame::Loop()
{
...
}

As part of the implementations, as well as all the other classes in your child class, which leads me to beleive either #2 above is the problem, or you need to do some brushing up on C++ Classes! [wink] Good luck!

Share this post


Link to post
Share on other sites
ARGHHHH
How can I make such a stupid mistake!!

I was very close, I tought I might needed to call the Loop function this way: LoadFrame::Loop() but that ofcourse didn't worked. All other functions are defined as they should be (with the LoadFrame::) so I don't know why I did this one wrong :/


Anyway thanks alot, you saved me again :D

Share this post


Link to post
Share on other sites

This topic is 4597 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this