2

I am not sure why I am getting an "error C2660: 'SubClass::Data' : function does not take 2 arguments". when i try to compile my project.

I have a base class with a function called data. The function takes one argument, There is an overload of Data that takes 2 arguments. In my subClass I override the data function taking 1 argument. Now when I try to call the overload of data from a pointer to subClass I receive the above compile error.

class Base : public CDocument
{
public:
   virtual CString& Data( UINT index);      
   CString Data(UINT index, int pos);   
};

class SubClass : public Base
{
public:
   virtual CString& Data(UINT index);       
};

Void SomeOtherFunction()
{
   subType* test = new subType();
   test->Data( 1, 1);// will not compile
   ((Base*)test)->Data(1,1); // compiles with fine.
}
TonySalimi
  • 8,257
  • 4
  • 33
  • 62
Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
  • 3
    What is `subType`? Is it any relation to `SubClass`, also `base` vs `Base`? Please post the _actual_ code that you are using. – CB Bailey Dec 02 '09 at 21:43
  • Possible duplicates: http://stackoverflow.com/questions/411103/ http://stackoverflow.com/questions/1480085/ http://stackoverflow.com/questions/1799497/ – CB Bailey Dec 02 '09 at 21:46
  • Yeah, this question comes up a lot. Here's another one: http://stackoverflow.com/questions/888235/overriding-a-bases-overloaded-function-in-c – Fred Larson Dec 02 '09 at 21:52

5 Answers5

9

The C++ Programming Language by Bjarne Stroustrup (p. 392, 2nd ed.):

15.2.2 Inheritance and Using-Declarations
Overload resolution is not applied across different class scopes (§7.4) …

You can access it with a qualified name:

void SomeOtherFunction()
{
  SubClass* test = new SubClass();

  test->Base::Data(1, 1);
}

or by adding a using-declaration to SubClass:

class SubClass : public Base
{
  public:
  using Base::Data;
  virtual CString& Data( UINT index);
};

void SomeOtherFunction()
{
  SubClass* test = new SubClass();

  test->Data(1, 1);
}
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
3

Your override of Data(UINT index) in SubClass 'hides' the overload in the base class.

A solution is to code SubClass like this:

class SubClass : public Base
{
public:
using Base::Data;    // <--- brings all the overloads into scope
virtual CString&    Data( UINT index);          
};

Now test->Data(1,1) should work.

quamrana
  • 37,849
  • 12
  • 53
  • 71
1

You have to add using Base::Data in your SubClass

Take a look at Item 33: Avoid hiding inherited names of Effective C++

Nikola Smiljanić
  • 26,745
  • 6
  • 48
  • 60
1

It's the hiding rule trying to protect you from yourself.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
0

When you override the function declared in the base class, all other functions with that name becomes hidden in the subclass. As mentioned previously, you would need to bring those names into the scope of the subclass through the using Base::Data declaration.

jasonline
  • 8,646
  • 19
  • 59
  • 80