Skip to main content
GameDev.net gamedev.net
🔒 Locked

[OOP] problem

Started by cold_heats_.--. Jul 7, 2012 at 5:08 PM 3 replies 2.4k views
Original Post
cold_heats_.--.
cold_heats_.--.
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 :

#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


The header for the derived class:

#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


Definitions for the derived class:

#include "CMainMenu.h"

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


And the main :

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


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 ?
ToohrVyk
ToohrVyk
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 :

virtual void modify() = 0;
Narf the Mouse
Narf the Mouse
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).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.