By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

Under what conditions is it impossible to put an inspector method in a source file?

P: n/a
Hi,

I am learning, a little late, about const correctness and have found some strange behavior which I just don't understand. I wrote a class which has a std::list of ints, & a method to return a bool, true if the list contained the int given as the method's argument. It seemed obvious that it could get const at the end, since it was not changing anything about the object.

However, I moved the method into the .cpp file and suddenly the compiler refused to identify "bool hasThisInt( int const inputInt ){...}" in the .cpp file with its prototype "bool hasThisInt( int const inputInt ) const;" in the header. If I removed the const at the end of the prototype, the compiler stopped complaining. However, now the method counts as a mutable, so this is not ideal (since, as you might guess, this is actually a toy example for a larger, more complicated program).

Is this related to the issues preventing template classes being split across headers and source files? Or is it some peculiarity of my compiler? I am using g++ 4.2 on Ubuntu, through Eclipse Helios with the C++ IDE 1.3.1.

Just to recap:
it works if the method is split across header and source, as long as it is not declared const;
it also works if the method is entirely in the header and declared const;
but it does not work if the method is split across header and source while also declared const.

Thank you very much for your answers,
Ben O'Leary (Hey, why does bytes.com hate Irish names? Any attempt to put some sort of indicator in its place results in an invalid name too.)

Here is the offending code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. in the header file inspectionExample.h:
  3.  
  4. #ifndef INSPECTIONEXAMPLE_H_
  5. #define INSPECTIONEXAMPLE_H_
  6.  
  7. #include <vector.h>
  8.  
  9. class inspectionExample
  10. {
  11.  
  12. public:
  13.  
  14.   inspectionExample( int x,
  15.                      int y,
  16.                      int z );
  17.  
  18.   virtual
  19.   ~inspectionExample();
  20.  
  21.   bool
  22.   hasThisInt( int const inputInt )
  23.   const
  24.   {
  25.  
  26.     bool returnBool = false;
  27.  
  28.     for( std::vector< int >::const_iterator intIt = ints.begin();
  29.          ints.end() > intIt;
  30.          ++intIt )
  31.       {
  32.  
  33.         if( inputInt == *intIt )
  34.           {
  35.  
  36.             returnBool = true;
  37.  
  38.           }
  39.  
  40.       }
  41.  
  42.     return returnBool;
  43.  
  44.   }
  45.  
  46. protected:
  47.  
  48.   std::vector< int > ints;
  49.  
  50. };
  51.  
  52. #endif /* INSPECTIONEXAMPLE_H_ */
  53. (end of inspectionExample.h)
  54.  
  55. If I move hasThisInt to inspectionExample.cpp, this is what is in the source file
  56. (the constructor and destructor are here in any case):
  57.  
  58. #include "inspectionExample.h"
  59.  
  60. inspectionExample::inspectionExample( int x,
  61.                                       int y,
  62.                                       int z )
  63. {
  64.  
  65.   ints.push_back( x );
  66.   ints.push_back( y );
  67.   ints.push_back( z );
  68.  
  69. }
  70.  
  71. inspectionExample::~inspectionExample()
  72. {
  73.   // does nothing.
  74. }
  75.  
  76.  
  77. bool
  78. inspectionExample::hasThisInt( int const inputInt )
  79. {
  80.  
  81.   bool return_bool = false;
  82.  
  83.   for( std::vector< int >::const_iterator intIt = ints.begin();
  84.        ints.end() > intIt;
  85.        ++intIt )
  86.     {
  87.  
  88.       if( inputInt == *intIt )
  89.         {
  90.  
  91.           return_bool = true;
  92.  
  93.         }
  94.  
  95.     }
  96.  
  97.   return return_bool;
  98.  
  99. }
  100.  
  101. (end of inspectionExample.cpp)
  102.  
  103.  
Nov 25 '10 #1
Share this Question
Share on Google+
1 Reply


100+
P: 687
'const' after argument list is a part of declaration and must be present in .cpp as well
bool inspectionExample::hasThisInt( int const inputInt ) const
{
...
}
Nov 26 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.