no appropriate default constructor

D

Duy Lam

The compiler is complaining about "no appropriate default constructor
available" when I reference a subclass.

The basic setup is that i have a class Test and a subclass called
TestKid. I want to create a TestKid within a function of my Test
class. It gives me this error, though, and I can't figure out why
because I DO have default constructors. Can someone help me out?
Thanks.

all the files:
Test.h
------
class TestKid;
class Test {
public:
void make();
Test();
virtual ~Test();
};

Test.cpp
--------
#include "Test.h"
Test::Test() { }
Test::~Test() { }
void Test::make() { TestKid* t = new TestKid(); }

TestKid.h
---------
#include "Test.h"

class TestKid : public Test {
public:
TestKid();
virtual ~TestKid();
};

TestKid.cpp
-----------
TestKid::TestKid() { }
TestKid::~TestKid() { }


This is puzzling me greatly...
 
V

Victor Bazarov

Duy said:
The compiler is complaining about "no appropriate default constructor
available" when I reference a subclass.

The basic setup is that i have a class Test and a subclass called
TestKid. I want to create a TestKid within a function of my Test
class. It gives me this error, though, and I can't figure out why
because I DO have default constructors. Can someone help me out?
Thanks.

all the files:
Test.h
------
class TestKid;
class Test {
public:
void make();
Test();
virtual ~Test();
};

Test.cpp

Add

#include "TestKid.h"
Test::Test() { }
Test::~Test() { }
void Test::make() { TestKid* t = new TestKid(); }
^^^^^^^^^
The compiler needs to know the definition of 'TestKid' class here to
produce the code.
TestKid.h
---------
#include "Test.h"

class TestKid : public Test {
public:
TestKid();
virtual ~TestKid();
};

TestKid.cpp
-----------
TestKid::TestKid() { }
TestKid::~TestKid() { }


This is puzzling me greatly...

It's OK, we're all learning.

V
 
E

enzo

Duy Lam ha scritto:
The compiler is complaining about "no appropriate default constructor
available" when I reference a subclass.

The basic setup is that i have a class Test and a subclass called
TestKid. I want to create a TestKid within a function of my Test
class. It gives me this error, though, and I can't figure out why
because I DO have default constructors. Can someone help me out?
Thanks.

all the files:
Test.h
------
class TestKid;
class Test {
public:
void make();
Test();
virtual ~Test();
};

Test.cpp
--------
#include "Test.h"
Test::Test() { }
Test::~Test() { }
void Test::make() { TestKid* t = new TestKid(); }

TestKid.h
---------
#include "Test.h"

class TestKid : public Test {
public:
TestKid();
virtual ~TestKid();
};

TestKid.cpp
-----------
TestKid::TestKid() { }
TestKid::~TestKid() { }


This is puzzling me greatly...

The compiler doesn't know class TestKid while compiling Test.cpp...in
forward declaration you can only create pointers and references to it.
An attempt to instantiate it or use it in a sizeof expression will cause
a compilation error.
 
D

Duy Lam

Hmmm, okay I understand what you're saying, but if I put "#include
"TestKid.h"" in "Test.h" it gives me a "base class undefined" message,
which I assume is because Test.h it will look at TestKid.h and it
makes a reference to Test ..... which hasn't been defined yet.... I
figured this circular "include" wouldn't work. How do I solve this?
Thanks!

Duy Lam
 
J

John Harrison

Duy Lam said:
Hmmm, okay I understand what you're saying, but if I put "#include
"TestKid.h"" in "Test.h" it gives me a "base class undefined" message,
which I assume is because Test.h it will look at TestKid.h and it
makes a reference to Test ..... which hasn't been defined yet.... I
figured this circular "include" wouldn't work. How do I solve this?
Thanks!

Don't put #include "TestKid.h" in Test.h, put it in Test.cpp (after #include
"Test.h"), that is where it is needed.

And don't top post.

john
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top