You are here:

C++/Using the STL

Advertisement


Question
Hellow Ralph,
 I'm trying to reteach myself classes and ran into a funny problem (although I don't think it's classes related).  My code looks as follows (i'll leave the trivial stuff out)
*********************
StatPackMain.cpp
...
#include <statArray.h">
...
int test[4] = {4,2,0,5};
statArray(4, test); //declaring an instance of statArray
...
**********************
statArray.h
class statArray {
...
public
 statArray(int, int[]); //my constructor
...

}
**********************
statArray.cpp
#include <algorithm>   //<--this i'm not sure about
#include "statArray.h"
statArray::statArray(int size, int array[]) {
 ...
 sort(array, array + size);
 ...
}
**********************


So, my problem is that my compiler says that "sort(...)" is undeclared.  I'm not really sure what to do, since I already included <algorithm> which I've read is supposed to contain a sort() function.  Also, if I put a #include <alogrithm> into the StatPackMain.cpp and try a sort() there, I get no error (provided that I remove the call in statArray.cpp).  Any thoughts?

I'm running Dev-C++ 4.9.9.2 on WinXP SP2 using
and I think the MinGW release is 3.4.2 but I could be wrong.  It's whatever came with the Dev package.  

Answer
First off the STL sort, like all C++ library names, is in the namespace std i.e. it is properly called std::sort. You can bring the name sort into another namespace, such as the global (unnamed) namespace, using either a using declaration to bring in one name:

using std::sort;
Or a using directive to bring in all (known) names from a namespace:

using namespace std;

Note that you should _not_ place these in header files.

For more information see Herb Sutter's book More Exceptional C++ Items 39 and 40, and his Guru of the Week (GotW) article #53 at http://www.gotw.ca/gotw/053.htm.

Either of the two using statements will place std::sort into the global unnamed namespace so it can be called without the std:: qualification.

Why it should work in main and not in the class constructor I cannot say unless your StatPackMain.cpp file contains a using declaration or directive before sort is used. I pieced together the following code (in one source file) from your code (which has a few typos by the way!) adding only std:: in front of the call to sort in your statArray constructor:

#include <algorithm>
#include <iostream>

// Helper function to print array values
void PrintArray( int * p, int size )
{
   for ( int i=0; i < size; ++i )
   {
         std::cout << *(p+i) << " ";
   }
}


// Cut down implementation of your statArray class
// Copied, pasted and made to compile from question code
class statArray
{
public:
   statArray(int, int[]);

};

statArray::statArray(int size, int array[])
{
   std::sort(array, array + size); // sort qualified with std::
}


// Main Entry Point
int main()
{
   int const size(4);    // Array size constant

   // First method:
   // ------------
   // Define unsorted array, print it, sort it, print it again
   int array[size] = {3,1,2,0};

   PrintArray( array, size );
   std::cout << '\n';

   std::sort(array, array + size );

   PrintArray( array, size );
   std::cout << '\n';

   // Second method:
   // ------------
   // Define unsorted array, print it,
   // create statArray instance with it, print it again
   int array2[size] = {3,1,2,0};

   PrintArray( array2, size );
   std::cout << '\n';

   statArray sa( size, array2 );

   PrintArray( array2, size );
   std::cout << std::endl;

   system( "pause" ); // Wait for user to read result!
}

It was compiled and run under Windows using MS VC++ 8 (2005) and under SuSE Linux 10.0 with g++ 4.0.2. With the exception that Linux does not know about the MS CMD shell specific pause command they both produce the same results:

3 1 2 0
0 1 2 3
3 1 2 0
0 1 2 3

Finally you might like to look at the STL container types: std::vector in particular might be a useful replacement for built in arrays.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

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.

Experience

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 http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.