 |
BOOK: Professional C++, 2nd Edition  | This is the forum to discuss the Wrox book Professional C++, 2nd Edition by Marc Gregoire, Nicholas A. Solter, Scott J. Kleper ; ISBN: 978-1-1181-6995-7 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional C++, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

April 18th, 2016, 10:46 PM
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Chapter 8, page 224
The third paragraph on page 224 states the following:
"The following code shows a version of Super that lacks a default constructor. The associated code version of Sub must explicitly tell the compiler how to call the Super constructor or the code will not compile"
Code:
class Super
{
public:
Super(int i = 0);
};
class Sub : public Super
{
public:
Sub();
};
Sub::Sub() : Super(7)
{
// Do Sub's other initialization here.
}
How does Sub explicitly tell the compiler to call the Super constructor? I inferred that the default constructor was required to be called.
|

April 20th, 2016, 03:39 PM
|
Wrox Author
|
|
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
First, the code you are posting is not the same as in the book.
You have specified a default value for the parameter in the Super constructor and thus your Super class has in fact a default constructor.
Anyway, in the book itself, there is no default value for the parameter in the Super constructor and thus Sub has to explicitly tell the compiler how to call the Super constructor. This is done with the ": Super(7)" after Sub::Sub().
|

April 21st, 2016, 02:07 AM
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Chapter 8, page 224
Marc,
Thank you for finding my error and for the explanation that I actually had a default constructor because of the default value. However, I still do not understand what is going on.
Your response indicates that Super(int i) a constructor, and hence, by its syntax, it is not a default constructor.
Obviously by its syntax, Sub() is a default constructor, and
Code:
Sub::Sub() : Super(7)
{
// Do Sub's other initialization here.
}
is the definition of Sub's default constructor.
Super(7) uses Super(int i) as its constructor.
The text says the associated version of Sub must explicitly tell the compiler how to call the Super constructor or the code will not compile. What needs to be done to get the code to compile?
My corrected code does not compile.
|

April 21st, 2016, 01:23 PM
|
Wrox Author
|
|
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
What the text is saying is that all constructors of Sub *must* explicitly call the Super constructor with a parameter. For example:
Code:
Sub::Sub() : Super(7)
{
// Do Sub's other initialization here.
}
This is the correct code. This Sub default constructor calls the Super constructor with argument 7.
However, the following will not compile:
Code:
Sub::Sub()
{
// Do Sub's other initialization here.
}
I removed the " : Super(7)", so you are not explicitly calling the Super constructor anymore.
If the first version with ": Super(7)" does not compile for you, then there must be another error somewhere else.
|

April 21st, 2016, 10:59 PM
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Chapter 8, page 224
Marc,
Thank you for clarifying what the text is saying.
I went through my code several time and did not find the error.
Code:
// Super class
class Super
{
public:
Super(int i);
};
class Sub : public Super
{
public:
Sub();
};
Sub::Sub() : Super(7)
{
// Do Sub's other initialization here.
}
and here is the error message of which I do not understand.
Code:
1>------ Build started: Project: SuperClass, Configuration: Debug Win32 ------
1> SuperClass.cpp
1>SuperClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Super::Super(int)" (??0Super@@QAE@H@Z) referenced in function "public: __thiscall Sub::Sub(void)" (??0Sub@@QAE@XZ)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>F:\Professional C++\Chapter 8\SuperClass\Debug\SuperClass.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
|

April 26th, 2016, 02:04 PM
|
Wrox Author
|
|
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
You still need to implement your Super(int i) constructor.
For example as follows (just an empty implementation):
Code:
Super::Super(int i) { }
|

April 27th, 2016, 09:38 AM
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Chapter 8, page 224
Marc,
I added the constructor as suggested to the file:
Code:
// Super class
class Super
{
public:
Super(int i);
};
class Sub : public Super
{
public:
Sub();
};
Sub::Sub() : Super(7)
{
// Do Sub's other initialization here.
}
Super::Super(int i) { }
But, get the following errors:
Code:
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup D:\Professional C++\Chapter 8
\SuperClass\SuperClass\MSVCRTD.lib(crtexe.obj) SuperClass
Error 2 error LNK1120: 1 unresolved externals D:\Professional C++\Chapter 8\SuperClass\Debug\SuperClass.exe 1 1 SuperClass
|

April 30th, 2016, 04:27 AM
|
Wrox Author
|
|
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
You seem to be missing a main() function. For example:
|

April 30th, 2016, 09:40 PM
|
Authorized User
|
|
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
|
|
Chapter 8, page 224
Thank you Marc,
I separated the class definition into a header file, and the implementation of the methods into a cpp file. Then, I made a cpp file for int main(). It worked.
Now I know that when I encounter a similar error message, I need a file for int main().
|
|
 |