You are here:

C++/undefined reference


What exactly is a "undefined reference" error?

I have created a class called GridPDF. it resides in grid_pdf.cpp and grid_pdf.hpp I have created a test program filtertest.cpp which has #include "grid_pdf.hpp" and is compiled with grid_pdf.o

problem occurs while compiling, I get this:

/grid_pdf.cpp:90: undefined reference to `GridPDF::create_axis_vectors(boost::numeric::ublas::vector<double, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >, boost::numeric::ublas::vector<double, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >, int, int, int, int)

at grid_pdf.cpp:90 I have this:

this->create_axis_vectors(x_axis_vector, y_axis_vector, xmin, xmax, ymin, ymax);

where x_axis_vector and y_axis_vector are of type 'Vector' which comes from the uLapack library and worked fine in this context before making the class and splitting up my program.

I cant seem to find where I have introduced this bug.

Many Thanks

As error messages differ between tools, and you do not say what compiler and linker you are using I am going to have to guess. Mentioning what compiler and linker you are using would have been courteous, especially as you expect a reasonable answer!  Its not VC8, I think that it maybe a GNU tool chain - g++ and the GNU linker.

OK, so you call some member function on a GridPDF instance. Where is this function declared and where is it defined (implemented)?

If the compiler cannot find a match for this function it will complain. However you will hopefully get a more helpful error such as:
no matching function call to `GridPDF::create_axis_vectors(boost::numeric::ublas::vector<double, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >, boost::numeric::ublas::vector<double, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >, int, int, int, int)'.

This leads us to the second option: that the class does indeed have such a member function declared in the class definition but you failed to actually implement it (define it) in the class implementation file (grid_pdf.cpp). In this case the compiler is happy - for all it knows the definition is in another compilation unit - grid_pdf2.cpp maybe. In this case it would be the linker that complains. In these cases the errors usually refer to unresolved references or undefined references and often cannot show such a good correspondence to the C++ names, usually showing the mangled version of the symbol name. The GNU linker is an exception. It does return errors of the form you show if you are building with debugging information. You should see something like:

collect2: ld returned 1 exit status

Right at the end of the error messages which indicates that the GNU linker ld generated the errors.

One way to not implement a class member function is to implement it but forget to prefix the class name to it so instead of:

void GridPDF::create_axis_vectors( ... )

You forget the GridPDF:: and write:

void create_axis_vectors( ... )

I have made this mistake many times. Usually the function does not compile because it cannot access the class instance context and non-public members. If you are unlucky it will compiler but in fact defines a non-member function. I say unlucky as the problem is then only picked up during linking and it is best to get errors reported as early as possible, as the compiler can generally give more contextual information.

If it does build as a non-member function it begs the question of whether the function needs to be an instance member function at all.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.