You are here:

C++/Calling pivate class members functions


Thanks for the speedy reply on this Ralph.  I am using GNU complier, nice one.  I am using g/+/+ 4.0 or something, i.e recent.

Thanks, this made heaps of sense. however I am still stuck.

After hours of trying to find out what I had done wrong, it seems that I do not understand Classes properly.  I was calling a class function from a class function, and it didn't like the way I was doing it.

If I want to make use of a function I have created say "make_axis_numbers(foo)" how do I do it and where do I put it?

I made a class as I said before called GridPDF and in it had GridPDF::create_axis_vectors(foo){foobar}

What I did before was make GridPDF::create_axis_numbers(foo){foobar} and called this function from GridPDF::create_axis_vectors(foo)
 do stuff
 do more stuff

this obviously does not work.

how are you supposed to do it?

thanks so much

:o) Phil


Followup To

Question -
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

Answer -
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.

But this is outside of my expertise.

First I have to say that your examples are not valid C++ - I expect you shortened them for the purposes of the question, but this does _not_ help me understand where you are going wrong. I also have no idea this time what the error is – last time the calling function (create_axis_vectors) was the problem, you showed the error and I managed to recreate a similar problem that generated that error. Now you say it is something within create_axis_vectors. I am not clairvoyant. Having to guess what you are doing makes answering your questions very much harder – i.e. takes more time and thought away from what I am doing...and I still might not get where you have gone wrong. So I am more likely to give you short shrift.

Anyway, there is nothing wrong with what you are trying to do – calling a private helper from another member function is common and valid C++ practice. I would expect to see something like so:

In the class header file:

   class GridPDF
     // ...

       void create_axis_vectors( FooType const & foo );

     // ...

     // ...

     // Declaration of create_axis_numbers
       void create_axis_numbers( FooType const & foo );

     // ...

And in the class implementation file:

   void GridPDF::create_axis_vectors( FooType const & foo )
   // ...

       create_axis_numbers( foo );

   // ...

  // Definition of create_axis_numbers
   void GridPDF::create_axis_numbers( FooType const & foo )
   // ...


   // ...

is a placeholder for where additional un-shown code may be. If this is basically what you have then your code should work. It built for me using:

   typedef int FooType;

to define FooType as an alias for int and creating a GridPDF and calling create_axis_vectors thus:

   int main()
       GridPDF g;
       FooType foo( 1 );
       g.create_axis_vectors( foo );

I used both g++ 4.0.2 and MSVC++ 8.0 (aka 2005). Note: for ease of getting up and running I had all the code in one source file – no headers, but this should not be a problem for you.

If however your code still does not build and none of my previous comments seem to apply then you most likely have not implemented the function you think you have or you are not calling the function you think you are. Here function may be either a class member function or a standalone function in the global namespace. If you are still getting what appears to be an error from ld then I suspect you are trying to call a function that you do not think you are. Again I cannot be much more help as you have not given me details.

Further more I am not interested in wading through reams of your code trying to sort out your build errors – I have enough of my own. If you are going to post code then please distil the problem down to the absolute minimum, preferably with reliance on nothing more than standard C++ facilities (e.g. no Boost uBLAS libraries) – I may well not use the libraries you do and sorting out this kind of thing can take a long time to get setup so I am more likely to give you a "it is too long and involved" stock reply. I also need to know the build environment – compiler etc. OS and the like, but you got that last time didn’t you <g>?

You may well find that in the process of distilling the code down to the minimum you solve the problem yourself. In fact this would be my advise for trying to sort out what is going on. You can (temporarily) comment out the existing code and replace both the call to and the declaration and definition of create_axis_numbers with simpler functions such as the one above. In fact start out with a function taking no parameters and work up from there through say taking one int then one more complex type such as a Boost numeric uBLAS library unbounded_array – which is a class template type and may well introduce additional complexity. The idea is to reduce the number of things the compiler does not like until your code builds. You can modify this approach in line with additional information received – such as which errors you get.  


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


©2017 All rights reserved.