473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

no appropriate default constructor

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...
Jul 22 '05 #1
4 2301
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
Jul 22 '05 #2
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.

Jul 22 '05 #3
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 <re****************@tiscali.it> wrote in message news:<pw***********************@news3.tin.it>...
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.

Jul 22 '05 #4

"Duy Lam" <du****@hotmail.com> wrote in message
news:d3*************************@posting.google.co m...
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
5
by: Ook | last post by:
Here is my code, can some kind soul tell me what I'm doing wrong, or why I get this compile error, and maybe what to do to prevent it? I think I must be missing some fundamental concept here, or...
4
by: NoNickName | last post by:
Here's what I've got, essentially: main() { class derClassA *dCA; dCA = new(derClassA); } class derClassA {
3
by: Stanislav Simicek | last post by:
Hello, I'm trying to implement "Null-Field" feature (like Stream::Null) in MC++, but I am not able to initialize static member properly due to compiler error C2512 (no appropriate default...
0
by: WithPit | last post by:
I have some problems with the instantiating new objects. For example I have the following code //Headerfile (Texture1D.h> #pragma once #using <mscorlib.dll> #include <osg/Texture1D> #include...
3
by: denis_browne | last post by:
When writing the following code: class Base { Base(const Base &rhs) {} ~Base(); }; void f() { Base b;
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.