inheritance problems
Dear all,
I just can''t figure out why i can''t get my inheritance thing working. For instance, my base class is Ctga (tga.h).
In my derived class (CtgaGL) i include the header for Ctga and start the class definition with:
class CtgaGL : public Ctga
despite this, in "main", I can''t access the public methods from Ctga. I can access the public methods in the derived class "CtgaGL" but not its base class. I just cant figure out whats gone wrong! Ive included a sample of my code below!
Any suggestions?
Many thanks in advance!!
All help is __MUCH__ appreciated!!!
In "main.cpp"
#######################################################
#include "tgaGL.h"
float tgaStuff(void)
{
CtgaGL img;
// this works okey
if( img.Load("data/star.tga" ) != 1)
return 0;
// but I cant access methods in Ctga
// for instance
img.GetHeight(); <- just cant access this method!!
#######################################################
#################### CtgaGL.h #######################
#include "tga.h"
class CtgaGL : public Ctga
{
public:
CtgaGL(); // constructor 1
~CtgaGL() { } // destructor
GLint Load(char *name);
....
protected:
....
};
########################################################
######################## tga.h #########################
class Ctga
{
public:
Ctga();
Ctga(char *name);
int GetHeight(void) {return height;}
....
protected:
int height;
};
########################################################
I''d say it would work.
Probably there''s something you''ve not seen.
For instance, are you sure that there''s not a ''private'' or ''protected'' between Ctga(char *name) and int GetHeight(void) ?
Anyhow, you should get rid of the ''void'', ie your method shall be "int GetHeight()". Using ''void'' as ''zero parameter'' is C syntax, not C++. That may be your problem.
Otherwise, would you please post the detailed line that your compiler writes ?
Probably there''s something you''ve not seen.
For instance, are you sure that there''s not a ''private'' or ''protected'' between Ctga(char *name) and int GetHeight(void) ?
Anyhow, you should get rid of the ''void'', ie your method shall be "int GetHeight()". Using ''void'' as ''zero parameter'' is C syntax, not C++. That may be your problem.
Otherwise, would you please post the detailed line that your compiler writes ?
March 19, 2002 09:10 PM
It looks like you haven''t declared your functions as virtual functions. That would prevent your derived classes from being able to use them. Just say:
<pre>
######################## tga.h #########################
class Ctga
{
public:
Ctga();
Ctga(char *name);
virtual int GetHeight(void) {return height;}
....
protected:
int height;
};
########################################################
</pre>
and now anything that is a subclass of Ctga will be able to call that same GetHeight function. You are also free to overwrite it as well, but by default it will resort to the same implementation of its base class.
As for using "int GetHeight(void) {...}", that doesn''t affect anything you''re describing. From what I understand, most programmers don''t use it because it is implied by (), but I have heard some people say it''s good practice to make it obvious to those reading it that it takes no parameters. *shrug* It''s up to you.
Best ...
eriol
<pre>
######################## tga.h #########################
class Ctga
{
public:
Ctga();
Ctga(char *name);
virtual int GetHeight(void) {return height;}
....
protected:
int height;
};
########################################################
</pre>
and now anything that is a subclass of Ctga will be able to call that same GetHeight function. You are also free to overwrite it as well, but by default it will resort to the same implementation of its base class.
As for using "int GetHeight(void) {...}", that doesn''t affect anything you''re describing. From what I understand, most programmers don''t use it because it is implied by (), but I have heard some people say it''s good practice to make it obvious to those reading it that it takes no parameters. *shrug* It''s up to you.
Best ...
eriol
mind me, but the virtual keyword won't help.
Not that it's a bad thing, but it simply should not correct the problem, otherwise the compiler is _very_ bad.
About the void keyword, it may help because it's not C++ standard to write it. Some compilers detects an error at this point, and that's not an implementation error from the compiler.
Anyhow, I admit it's minor. But when there's an unexpected problem, every minor problem becomes a clue !
[edited by - vincoof on March 20, 2002 2:44:26 AM]
Not that it's a bad thing, but it simply should not correct the problem, otherwise the compiler is _very_ bad.
About the void keyword, it may help because it's not C++ standard to write it. Some compilers detects an error at this point, and that's not an implementation error from the compiler.
Anyhow, I admit it's minor. But when there's an unexpected problem, every minor problem becomes a clue !
[edited by - vincoof on March 20, 2002 2:44:26 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement