no appropriate default constructor

Discussion in 'C++' started by Duy Lam, Jun 10, 2004.

  1. Duy Lam

    Duy Lam Guest

    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...
     
    Duy Lam, Jun 10, 2004
    #1
    1. Advertising

  2. Duy Lam wrote:

    > 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"


    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
     
    Victor Bazarov, Jun 10, 2004
    #2
    1. Advertising

  3. Duy Lam

    enzo Guest

    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.
     
    enzo, Jun 10, 2004
    #3
  4. Duy Lam

    Duy Lam Guest

    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

    enzo <> wrote in message news:<pw2yc.308955$>...
    > 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.
     
    Duy Lam, Jun 12, 2004
    #4
  5. "Duy Lam" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jun 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    515
    Victor Bazarov
    Oct 11, 2004
  2. Richard Herring
    Replies:
    0
    Views:
    5,615
    Richard Herring
    Oct 12, 2004
  3. Ook
    Replies:
    5
    Views:
    413
    Valentin Samko
    Oct 29, 2005
  4. NoNickName
    Replies:
    4
    Views:
    254
    Jim Langston
    Nov 1, 2005
  5. Replies:
    3
    Views:
    500
    n2xssvv g02gfr12930
    Feb 16, 2006
Loading...

Share This Page