Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question on function overloading

0 views
Skip to first unread message

raksha

unread,
Nov 11, 2009, 8:18:30 PM11/11/09
to
Hi,

I have a class A which is the base class. I have class B which is
derived class of A.
Class A does not have a default constructor(reason it is a class in c+
+ library).
I am trying to overload a function of class A in class B and it gives
me an error saying there is no appropriate default constructor
available. Is there any way to solve this problem?

Thanks
Raksha

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

red floyd

unread,
Nov 12, 2009, 3:37:29 AM11/12/09
to
raksha wrote:
> Hi,
>
> I have a class A which is the base class. I have class B which is
> derived class of A.
> Class A does not have a default constructor(reason it is a class in c+
> + library).
This reason makes no sense.

> I am trying to overload a function of class A in class B and it gives
> me an error saying there is no appropriate default constructor
> available. Is there any way to solve this problem?

You have an error on line 42 of your code. Please post a minimum,
compilable example that exhibits the behavior in question.

Kacper Gazda

unread,
Nov 12, 2009, 3:48:48 AM11/12/09
to
{ Your quoting could have been half in size -- please remove irrelevant
material (greetings, signature, banner, etc.) from your quoting. -mod }

On 12 Lis, 02:18, raksha <raksha...@gmail.com> wrote:
> Hi,
>
> I have a class A which is the base class. I have class B which is
> derived class of A.
> Class A does not have a default constructor(reason it is a class in c+
> + library).
> I am trying to overload a function of class A in class B and it gives
> me an error saying there is no appropriate default constructor
> available. Is there any way to solve this problem?
>
> Thanks
> Raksha
>
> --

> [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]


> [ comp.lang.c++.moderated. First time posters: Do this! ]

Could you paste your class A and B implementation( showing the
problematic method)???
This could help me to solve your problem and identify the operation
that required the default constructor in A.

Thanks,

Kacper

Gerhard Menzl

unread,
Nov 12, 2009, 7:04:13 AM11/12/09
to
raksha wrote:

> I have a class A which is the base class. I have class B which is
> derived class of A.
> Class A does not have a default constructor(reason it is a class in

> c++ library).


> I am trying to overload a function of class A in class B and it gives
> me an error saying there is no appropriate default constructor
> available. Is there any way to solve this problem?

There sure is. Its first step is to post stripped down, minimal, and
compilable code that exposes the problem.

Terminological hint: you cannot overload a member function of a class in
a class dervied from it. This would be overriding (if it is a virtual
function) or hiding (if it isn't)

--
Gerhard Menzl

Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".

Goran

unread,
Nov 12, 2009, 7:09:03 AM11/12/09
to
On Nov 12, 2:18 am, raksha <raksha...@gmail.com> wrote:
> Hi,
>
> I have a class A which is the base class. I have class B which is
> derived class of A.
> Class A does not have a default constructor(reason it is a class in c+
> + library).
> I am trying to overload a function of class A in class B and it gives
> me an error saying there is no appropriate default constructor
> available. Is there any way to solve this problem?

class B : public A
{
public:
B() : A(whatever params A::A needs) {}
};

IOW: you must call appropriate constructor of A in whatever
constructor B might have. That must be the case happens even if A has
a default constructor, but then you don't have to spell it out,
compiler does it.

Goran.

raksha

unread,
Nov 12, 2009, 9:07:52 PM11/12/09
to
okay

here is an example...

class A // defined in the library...which i cant edit it
{

//no default constructor defined like A(){}
//there are other constructors with input arguments define like
A(int i) { };

//function in this class
int wait()
{
//do something
}

}

//header file of class B


class B: public A
{
public:

//overload the wait function
using A::wait;
int wait(int tokens);

}

//cpp file of class B
int B::wait(int tokens)
{
//do something different from other wait function
}

In the above example...since class A doesnt have a default constructor
it gives me an error saying default construtor not available...how can
i solve this

Raksha

dietma...@gmail.com

unread,
Nov 13, 2009, 1:59:23 AM11/13/09
to
On 12 Nov, 01:18, raksha <raksha...@gmail.com> wrote:
> I have a class A which is the base class. I have class B which is
> derived class of A.
> Class A does not have a default constructor(reason it is a class in c+
> + library).
> I am trying to overload a function of class A in class B and it gives
> me an error saying there is no appropriate default constructor
> available. Is there any way to solve this problem?

I don't think your problem is with overloading! The problem rather
seems to be that you don't know how to initialize a base class which
doesn't have a default constructor. This is simply done using a member
initializer list:

struct A { A(long); /* other members */ };
struct B: A
{
B(int);
private:
A m_value;
};
B::B(int value): // colon indicates start of member initializer list
A(value), // pass a value to the base class constructor
m_value(value) // pass a value to a data member's constructor
{

Kacper Gazda

unread,
Nov 13, 2009, 12:44:50 PM11/13/09
to
> [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]

> [ comp.lang.c++.moderated. First time posters: Do this! ]

Thank you for this minimum compilable example.
There is few questions I would like to ask according to the code you
have pasted:
1) why are You using the "using A::wait" in the B class body?
2) You would like to overload the the B's wait with A's wait?
3) All methods/data in class A are private??

Here is a sample of code which works, but I am not sure what you
really need and
what do you want to achieve.
Could you explain why are you designing the A,B classes the way you
showed ?

#include <iostream>

using namespace std;


class A
{
public:
A(int i) {cout<<"A ctor with value->"<<i<<"\n";}
int wait() { cout<<"I am in A::wait\n";}
};

class B: public A
{
public:

B(int i):A(i) { cout<<"B ctor with value->"<<i<<"\n";}
using A::wait;
int wait(int tokens) {cout<<"I am in B::wait,value->"<<tokens<<"\n";}
};

int main(int argc, char** argv)
{
A a(10);
a.wait();
B b(11);
b.wait();
return 0;
}

If you explain your design and give as much information as possible I
am sure we can help you
with your problem. But first justify your design and please explain
what do you want to achieve.


Thanks,

Kacper

red floyd

unread,
Nov 16, 2009, 4:04:09 PM11/16/09
to
On Nov 12, 6:07 pm, raksha <raksha...@gmail.com> wrote:
> okay
>
> here is an example...
>
> class A // defined in the library...which i cant edit it
> {
>
> //no default constructor defined like A(){}
> //there are other constructors with input arguments define like
> A(int i) { };
>
> //function in this class
> int wait()
> {
> //do something
>
> }
> }
>
> //header file of class B
> class B: public A
> {
> public:
>
> //overload the wait function
> using A::wait;
> int wait(int tokens);
>
> }
>
> //cpp file of class B
> int B::wait(int tokens)
> {
> //do something different from other wait function
>
> }
>
> In the above example...since class A doesnt have a default constructor
> it gives me an error saying default construtor not available...how can
> i solve this

You problem has absolutely nothing to do with the overload of wait().

It has to do with the fact that since B is derived from A, and you
provide no constructor
for B, it uses A's default constructor... which doesn't exist.

You need a B constructor that constructs the A portion using A::A
(int).

0 new messages