Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 June 8th, 2005, 01:24 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default [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?
  #2 (permalink)  
Old June 20th, 2005, 12:36 PM
Authorized User
 
Join Date: Oct 2004
Location: San Francisco, CA, .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
  #3 (permalink)  
Old June 30th, 2005, 06:37 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
  #4 (permalink)  
Old July 6th, 2005, 01:48 AM
Authorized User
 
Join Date: Oct 2004
Location: San Francisco, CA, .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
  #5 (permalink)  
Old July 8th, 2005, 09:09 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much, that solved the issue perfectly.
  #6 (permalink)  
Old October 8th, 2005, 10:27 AM
Registered User
 
Join Date: Oct 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

  #7 (permalink)  
Old October 8th, 2005, 11:46 AM
Registered User
 
Join Date: Oct 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #8 (permalink)  
Old October 9th, 2005, 05:40 PM
Authorized User
 
Join Date: Oct 2004
Location: San Francisco, CA, .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
 


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
Linker Error, Chapter 18 Spasticus BOOK: Ivor Horton's Beginning Visual C++ 2005 4 February 20th, 2008 05:04 AM
Linker error.help s_srivi1 Access VBA 4 November 12th, 2007 02:54 AM
Compiler linker error raja_welcomes Visual C++ 1 June 6th, 2007 11:17 AM
linker error while calling fortran from c rony C# 0 August 2nd, 2005 07:52 AM
linker error smukher Visual C++ 2 July 2nd, 2003 06:10 PM



All times are GMT -4. The time now is 05:43 AM.


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