inheritance problem

L

Lionel

I believe I must have a c++ syntax or packaging problem. I have to
reproduce my problem here as I can't disclose some of the original
information, hopefully I don't lose anything:

class A {
public:
A(char* someArg);
};

class B : public A {
public:
B(C *c, char* someArg);

};



b.cpp: In constructor `B::B(A*, char*)':
b.cpp:3: error: no matching function for call to `A::A()'
directory/a.h:7: note: candidates are: A::A(const A&)
directory/a.h:13: note: A::A(char*)

I hope that makes sense. Just as an aside, I am using qt, I think this
is a standard c++ problem and just a my lack of knowledge of the language.

Thanks,

Lionel.
 
J

Jim Langston

Lionel said:
I believe I must have a c++ syntax or packaging problem. I have to
reproduce my problem here as I can't disclose some of the original
information, hopefully I don't lose anything:

class A {
public:
A(char* someArg);
};

class B : public A {
public:
B(C *c, char* someArg);

};



b.cpp: In constructor `B::B(A*, char*)':
b.cpp:3: error: no matching function for call to `A::A()'
directory/a.h:7: note: candidates are: A::A(const A&)
directory/a.h:13: note: A::A(char*)

I hope that makes sense. Just as an aside, I am using qt, I think this is
a standard c++ problem and just a my lack of knowledge of the language.

Thanks,

Lionel.

My compiler would give a different error message for this, "no default
constructor for class a". Basically your class B derives from Class A,
hence Class A's constructor must be called when you create an object of
class B. But, you give no information in your class B constructor in how
to call class A's constructor. If class A had a defautl constructor the
compiler would call that.

So you have to include class a in class B's initialization list, like this:

class A {
public:
A(char* someArg);
};

class B : public A {
public:
B(C *c, char* someArg);

};

B::B(C *c, char* someArg): A(someArg) <-- Initialization list
{
whatever
};
 
L

Lionel

Jim said:
My compiler would give a different error message for this, "no default
constructor for class a".

That would have been nice :).
Basically your class B derives from Class A,
hence Class A's constructor must be called when you create an object of
class B. But, you give no information in your class B constructor in how
to call class A's constructor. If class A had a defautl constructor the
compiler would call that.

So you have to include class a in class B's initialization list, like this:

class A {
public:
A(char* someArg);
};

class B : public A {
public:
B(C *c, char* someArg);

};

B::B(C *c, char* someArg): A(someArg) <-- Initialization list
{
whatever
};

Nailed it! Clearly Java was clouding my vision here as it provides a
default constructor.

Thanks, now I can get on with the fun stuff :).

Lionel.
 
C

Carlos Martinez Garcia

Lionel said:
Nailed it! Clearly Java was clouding my vision here as it provides a
default constructor.

Thanks, now I can get on with the fun stuff :).

Lionel.

C++ provides default constructor too, but only when there is no other
constructor available
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top