Jump to content

  • Log In with Google      Sign In   
  • Create Account


[OOP] problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 cold_heats_.--.   Members   -  Reputation: 127

Like
0Likes
Like

Posted 07 July 2012 - 11:08 AM

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[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()
{
}

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 ?

Sponsor:

#2 ToohrVyk   Members   -  Reputation: 1587

Like
1Likes
Like

Posted 07 July 2012 - 12:08 PM

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;


#3 cold_heats_.--.   Members   -  Reputation: 127

Like
0Likes
Like

Posted 07 July 2012 - 01:16 PM

I defined them and it works. Thank you Posted Image

#4 Narf the Mouse   Members   -  Reputation: 318

Like
1Likes
Like

Posted 07 July 2012 - 10:16 PM

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).

#5 Saint Squireen   Members   -  Reputation: 181

Like
0Likes
Like

Posted 07 July 2012 - 10:24 PM

Hahahaha I get it! ;D

~Saint Squireen




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS