[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3585.0. "STL function objects" by NNTPD::"pdoyle@anorax.ilo.dec.com" (Paul Doyle) Thu May 22 1997 11:30

This function object example is straight from Musser &Saini, page 174:

#include <string>
#include <iostream.h>

class intGreater {
   public:
      bool operator()(int x, int y) const { return x > y; }
};

int main(int argc, char **argv)
{
   int x[10], i;

   for( i=0; i<10; i++ ) 
      x[i] = 20 * (4-i) - (i/2);

   cerr << "int array is : " << endl;
   for( i = 0; i < 10; i++ )
      cerr << i << ": " << x[i] << endl;
   
   sort(&x[0], &x[10], intGreater());

   cerr << "int array is : " << endl;
   for( i = 0; i < 10; i++ )
      cerr << i << ": " << x[i] << endl;
}

This fails to compile:

cxx: Error: ./cxx_repository/sort__XPiPi10intGreater.cxx, line 2: Ill-formed
parameter type list.
void static __dummy_ (int *p1, int *p2, intGreater p3)
----------------------------------------^

Any idea why?

Paul

[Posted by WWW Notes gateway]
T.RTitleUserPersonal
Name
DateLines
3585.1SPECXN::DERAMODan D'EramoThu May 22 1997 14:1614
        During linking it is realized that the instantiation file
        ./cxx_repository/sort__XPiPi10intGreater.cxx needs to be
        compiled.  That file uses intGreater but has no definition of
        intGreater.  That's because the template instantiation process
        expects class intGreater to be declared in one of the header
        files #include'd by the original source file.
        
        If you move the declaration of class intGreater to a header
        file which is included by the main .cxx program then the
        default template instantiation process should work.  Also,
        there is most likely a compiler command line qualifier to
        force the instantiation and using that should work too.
        
        Dan
3585.2stl function objects, templatesNNTPD::&quot;pdoyle@anorax.ilo.dec.com&quot;Paul DoyleMon May 26 1997 08:239
Dan, 

putting the intGreater declaration into a header file fixed the
problem for me - thanks for your help, and for the clear explanation 
of what was going wrong.

Paul

[Posted by WWW Notes gateway]