Why won't this compile?

Started by
4 comments, last by Kwizatz 11 years, 2 months ago

class Base
{
public:
virtual bool Load()=0;
bool Load(const char* filename);
};

bool Base::Load(const char* filename)
{
/* read file to some memory buffer member */
bool success = Load();
/* close file */
return success;
}

class Derived : public Base
{
public:
bool Load();
}

bool Derived::Load()
{
/* do whatever needs to be done */
return true;
}

SomeOtherClass::Load()
{
Derived* derived = new Derived;
derived->Load("somefile");
}

I am getting a "function does not take 1 arguments" on the call to Load in SomeOtherClass::Load(), if I change the name of the Load(const char*) function to something else or I downcast the derived pointer to the base class, it does compile and links fine.

Same error on Visual Studio 2010 and 2012

Any ideas?

Thanks!

Advertisement
Derived::Load() hides Base::Load(). You can bring in Base::Load() into Derived by putting a using Base::Load; in the Derived class definition.

You might want to reconsider what you're trying to do with this code and why it's structured this way.

Thank's SiCrane, I'll do that.

@0r0d: This code was a simple example of what I am doing, while the original code has to do with loading, this is not all there is to it, its not even a verbatim copy of the original code with complexity removed, explaining in detail what the original code does and why would be too lengthy a post I think, and not relevant. Suffice it to say, its part of resource loading and it is structured this way for 2 reasons: derived classes need only implement their specific resource type loading code and by keeping a data buffer inside the resource class to be filled from file or (zip compressed) memory, I avoid a (perhaps large) memcpy and releasing/using temporary buffer memory.

Suffice it to say, its part of resource loading and it is structured this way for 2 reasons...

Neither of which give any insight into why you would use the same name for both methods.

It would be much simpler (and clearer to the user!) if you named the virtual method something else - LoadInternal() springs to mind, but really, anything that isn't Load().

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Yes, that's how it used to be, LoadResource which was a protected method, its a good point, I'll think about changing it to something more descriptive.

This topic is closed to new replies.

Advertisement