Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 18th, 2016, 10:46 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default 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.
  #2 (permalink)  
Old April 20th, 2016, 03:39 PM
Wrox Author
Points: 220, Level: 4
Points: 220, Level: 4 Points: 220, Level: 4 Points: 220, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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().
  #3 (permalink)  
Old April 21st, 2016, 02:07 AM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default 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.
  #4 (permalink)  
Old April 21st, 2016, 01:23 PM
Wrox Author
Points: 220, Level: 4
Points: 220, Level: 4 Points: 220, Level: 4 Points: 220, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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.
  #5 (permalink)  
Old April 21st, 2016, 10:59 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default 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 ==========
  #6 (permalink)  
Old April 26th, 2016, 02:04 PM
Wrox Author
Points: 220, Level: 4
Points: 220, Level: 4 Points: 220, Level: 4 Points: 220, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You still need to implement your Super(int i) constructor.
For example as follows (just an empty implementation):
Code:
Super::Super(int i) { }
  #7 (permalink)  
Old April 27th, 2016, 09:38 AM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default 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
  #8 (permalink)  
Old April 30th, 2016, 04:27 AM
Wrox Author
Points: 220, Level: 4
Points: 220, Level: 4 Points: 220, Level: 4 Points: 220, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You seem to be missing a main() function. For example:
Code:
int main() { }
  #9 (permalink)  
Old April 30th, 2016, 09:40 PM
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default 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().
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 8 Ajax.ActionLink, at page 197. PartialView displayed as its own page. JimiFriis BOOK: Professional ASP.NET MVC 4 1 March 25th, 2015 04:18 AM
Chapter 8 - Page 232, Step 5 Building the Master Page bisigreat BOOK: Professional SharePoint 2010 Branding and User Interface Design 0 May 8th, 2013 12:58 PM
Chapter 9 - Try It OUT on page 319 Windblown BOOK: Beginning ASP.NET 4 : in C# and VB 6 January 29th, 2011 10:23 AM
Cookies ch6 p222-224 Alanrf BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 January 25th, 2010 03:55 PM
chapter 2 - page 41 problem creating the page.. jorjiana BOOK: Beginning Oracle Application Express ISBN: 9780470388372 2 August 22nd, 2009 04:25 AM



All times are GMT -4. The time now is 09:30 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.