Sign in to follow this  
FritzMar

Issue with inheritance

Recommended Posts

FritzMar    140
While i'm not new to programming i've been programming for the last 10 years since i was in 7th grade, and unfortunately i am self taught ( i see this as a good thing and a bad thing times) so here is my issue, I've been working on a mud code base on and off for the last few i decided to start back from scratch with the idea of actually planning it out and having goals so that i don't encounter feature creep and to make it easier to implement scripting so i got all my networking running and logins and stuff working so i sat down to create a base game object that would hold common settings to all obejects
 
//objects.h

class cObject
{
public:
    cObject();
    cObject(cObject & Object);
    ~cObject();

    int Save(cGwmlFile &File); 
    /*cGwmlFile is my markup language custom file libary */
    int Load(cGwmlFile &File);

    cBuffer Name;
    long ID;
protected:
};


and here is my second header file describing the player object, if i understand things correctly and i'm using the correct terminology *crosses fingers* it has an overloaded int save() which is passed (const char *) for a filename to open the selected cGwmlFile.
[source lang= "cpp"]

//player.h

#include "objects.h"

class cPlayer: public cObject
{
public:
    cPlayer();
    cPlayer(cPlayer & Player);
    ~cPlayer();

    int Load(const char * cpFileName);
    int Save(const char * cpFileName);
private:
};


now here is a quick example of what i want to do
[source lang = "cpp"]
//player.cpp

int cPlayer::Load(const char * cpFileName)
{
    cGwmlFile File;
    
    if(File.Open(cpFileName))
    {
        /* i load the player base file structure here
        and then i wanted to call the int cObject::Load(cGwmlFile & File)
        function to finish loading the default Object information 
        */

        Load(File); // <---- Error
        /*here is the error i'm getting

        error: no matching function for call to `cPlayer::Load(cGwmlFile&)'
        */
        return 1;
    }
    return 0;
}


i thought that because cPlayer is derived from cObject that would be a ble to the cObject::Load(cGwmlFile & File) from with in the overloaded cPlayer(const char * FileName) with out having to rewrite that portion of the code any help would be appreciated this is a cut down version of my code but it creates the same error i'm having from my main code base and was hoping that this would make it easier to find either a solution to my issue or help me find a work around thanks edited to put source in source blocks

Share this post


Link to post
Share on other sites
binchawpz    390
There is more info in the C++ FAQ. Though it may be useful to make the cObject class an abstract base class and just create a set of virtual functions for the subclasses to use. That way you don't have to mess around with specific calls. Plus, with the method you are currently using, things will get particularly messy if you are storing cPlayer specific data that the cObject save and load methods don't know anything about.

Share this post


Link to post
Share on other sites
FritzMar    140
binchawpz, thats one of the reasons i wanted to be able to derive from a base class was so that i could have it write out all the general stuff down all by it self cObject doesn't really care about what the name of the file is just that it exist so weather there is any other custom data for the player it just handles loading and saving the cObject specific data so i don't have to rewrite that part of my code ever time, I just have to define the player data to be saved/loaded then call load/save on the cObject to load the rest of the data :)

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