Sign in to follow this  
cold_heats_.--.

[OOP] problem

Recommended Posts

Hi.I'm fairly new to OOP and I can't seem to avoid this problem.
I created a base class which has data members and all the functions, except the constructor, are virtual.I want to derive new classes from it, but I get an error when compiling.

The header for the base class :
[CODE]
#ifndef GAMEAPP_H
#define GAMEAPP_H
#define nmax 128
#include<SDL/SDL.h>
#include<fstream.h>
class CGameApp
{
public:
SDL_Surface *backgv[nmax];
SDL_Surface *btnv[nmax];
char backgloc[nmax][100];
char btnloc[nmax][100];
ifstream backgf, btnf;
int backgn,btnn,currentbackg;
CGameApp(){};
virtual ~CGameApp(){};
virtual void modify();
virtual void initialize();
virtual void show();
virtual void free();
};
#endif
[/CODE]

The header for the derived class:
[CODE]
#ifndef MAINMENU_H
#define MAINMENU_H
#include "CGameApp.h"

class CMainMenu : public CGameApp
{
public:
CMainMenu(char *backgfile, char *btnfile)
{backgf.open(backgfile);btnf.open(btnfile);}
CMainMenu(){}
~CMainMenu(){backgf.close();btnf.close();}
void initialize();
void modify();
void execute();
void free();
void show();
};
#endif
[/CODE]

Definitions for the derived class:
[CODE]
#include "CMainMenu.h"

void CMainMenu :: initialize()
{
int i;
backgf>>backgn;
for(i=1;i<=backgn;i++)
backgf>>backgloc[i];
btnf>>btnn;
for(i=1;i<=btnn;i++)
btnf>>btnloc[i];
}
void CMainMenu :: modify()
{
if(currentbackg<backgn)
++currentbackg;
else
currentbackg = 1;
}
void CMainMenu :: free()
{
}
void CMainMenu :: execute()
{
}
void CMainMenu :: show()
{
}
[/CODE]

And the main :
[CODE]
#include "CMainMenu.h"
int main(int argc,char *argv[])
{
CMainMenu obj1;
//ofstream g("test.out");
return 0;
}
[/CODE]

The error is at the last line of the main source .The linker gives this .

E:/Programs/ProiecteSDL/BasicGameTemplate/main.cpp:8: undefined reference to `vtable for CGameApp'
obj\Debug\main.o(.text$_ZN8CGameAppD2Ev+0x3a):E:/Programs/ProiecteSDL/BasicGameTemplate/main.cpp:8: undefined reference to `vtable for CGameApp'

What am I doing wrong ?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
You declared functions modify, initialize, show and free in CGameApp, but you never defined them, which could be the cause for your error. Either provide a definition, or declare them as pure virtual using the following syntax :

[CODE] virtual void modify() = 0;[/CODE]

Share this post


Link to post
Share on other sites
Narf the Mouse    322
Also, your variable and function names are not clear. The only purpose of a variable or function name is to denote, clearly and simply, what the variable or function is.

Name your variables like they'll be read by a homicidal psychotic who knows where you live.

(That'll make more sense and be funny once you have more experience with debugging your own code three months later).

Share this post


Link to post
Share on other sites

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