 |
BOOK: Professional C++  | This is the forum to discuss the Wrox book Professional C++ by Nicholas A. Solter, Scott J. Kleper; ISBN: 9780764574849 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional C++ 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
|
|
|
|

June 8th, 2005, 01:24 PM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
[Linker error] undefined reference
So, I've been leafing though my copy of Professional C++ and working through it, but whenever I run some code which involves classes I get the error "[Linker error] undefined reference to `Records::employee::employee()'"
I have recieved this error by copying the code from the book, verbatim, and from running the code I downloaded from this website. I have compiled the code on Windows XP with DEV C++ from Bloodshed.net, and on Mandrake 10.2 with the built in C++ compiler.
Any ideas?
|
|

June 20th, 2005, 12:36 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi there,
A few possiblities:
* The error has employee as lower-case. Employee is a class, so in the book we capitalized it (Employee). Is it capitalized in all locations in your code?
* Did you download the code from this site to avoid a transcription error? You can get the sample code from the book at http://www.wrox.com/go/downloads
* Are you sure that you created a C++ project and not a C# or C project? This seems kind of unlikely, but it's worth checking out.
* Are you sure that all of the relevant source files are included with the project?
Please let us know if that helped or if you have resolved the problem some other way.
Scott J. Kleper
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|
|

June 30th, 2005, 06:37 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I downloaded the code samples from the site, unzipped the chapter 1 samples, and and double clicked Employee.h and EmployeeTest.cpp, which are located in the EmployeeDB folder. They opened up in Dev C++ so I removed the comments from the EmployeeTest.cpp file, (because the code was 'commented-out'), then I tried to compile it. Nope :(
Screenshots of the errors can be found on these two screenshots:
www.philarmstead.co.uk/disabled/employee.jpg
www.philarmstead.co.uk/disabled/employeetest.jpg
Thinking that the comments on the EmployeeTest.cpp might have been there for a reason, I also tried compiling Employee.cpp (which also relies on Employee.h), but I got the exact same errors.
|
|

July 6th, 2005, 01:48 AM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you for the additional info.
I downloaded devcpp and reproduced the error (I hadn't ever actually used this IDE before -- it's nice!)
The error you get is actually correct -- the IDE is compiling the EmployeeTest class. Since this class #includes "Employee.h", it gets the declaration for the Employee class and all of its associated methods. However, when the linker tries to generate the application, it can't find the *definition* of the Employee class and its methods. It knows the names, but never saw or compiled the implementations.
The solution is to add the Employee.cpp file to the project. From your description, it sounds like maybe you didn't create a project in Dev-C++. Doing so is necessary because otherwise it will just try to compile and run a single file. Here are the steps to create the project and run the example:
1) File -> New -> Project...
2) Choose "Console Application" and click OK
3) Save the project somewhere
4) The project starts you off with a main.cpp. In the "project" tab in the left sidebar, right click on 'main.cpp' and select "Remove File". The project is now empty.
5) Right click on Project1 (or whatever your project is called) under the 'project' tab in the left sidebar and select "Add to Project".
6) Navigate to the EmployeeDB folder and select Employee.cpp and click OK
7) Repeat step 6 selecting EmployeeTest.cpp
8) Execute -> Compile and Run
At this point, you'll see the files compile, link, and the console window should come up and disappear. That's because it runs so fast that you don't have a chance to see the output. To see the output, you can either run in debug mode, stepping through each line, or you can run the .exe file from a command window. My executable wound up in C:\Dev-Cpp\Project1.exe
Also, the reason that the main() is commented out in EmployeeTest is because the *real* program for EmployeeDB is found in UserInterface.cpp. I didn't want the two mains to collide but I wanted to leave the test code in for reference. I apologize if that was confusing. As long as you're just using Employee.cpp and EmployeeTest.cpp, go ahead and uncomment the code.
Hope this helps!
Scott
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|
|

July 8th, 2005, 09:09 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you very much, that solved the issue perfectly.
|
|

October 8th, 2005, 10:27 AM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I get the same error as described by thread starter. I am using Mandriva (former Mandrake) and the "built in" C++ compiler g++ version 3.4.3.
The problem is the same; only the declaration of the Employee constructer is included in the EmployeeTest file, so it can't find the definition of the Employee constructor (which is in the Employee.cpp file. If I include this file in the EmployeeTest file instead of the header file, it works fine.).
So the question is, how do link the program properly with this c++ compiler?
|
|

October 8th, 2005, 11:46 AM
|
|
Registered User
|
|
Join Date: Oct 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
After a bit of 'try and fail', I got it to work.
I compiled the Employee.cpp file first without linking it:
Code:
g++ -c Employee.cpp
Then I compiled the EmployeeTest.cpp and included the Employee.o file for the linker:
Code:
g++ EmployeeTest.cpp Employee.o -o test
Worked like a charm.
|
|

October 9th, 2005, 05:40 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Glad you got it to work, although the separation into two steps shouldn't be necessary. On Linux, the following command line successfully compiles and links the example for me:
Code:
g++ Employee.cpp EmployeeTest.cpp -o test
Of course, I had to uncomment main() in EmployeeTest.cpp, but it sounds like you've already done that.
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|
|
 |