473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
1 1221
newb16
687 512MB
'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

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

Similar topics

4
by: FrankIsHere | last post by:
Hi! I cannot figure out how to remove XHTML tags from an XML source file using XSLT. Here's what I have: source XML file: <?xml version='1.0'?> <Offer ID="S0173"...
7
by: Juarez | last post by:
Is it possible to divide a class in many .cs files ? For example one source file for each method ? Thanks.
5
by: TC | last post by:
Hello, I am familiar with obtaining source file reference material from the system clipboard using the old Win32 API. For example, copy an Excel chart onto the clipboard and goto Word. Under,...
1
by: LidorP | last post by:
Hello All, My problem is as follows: I have an assembly which holds a number of WebForms. In order to use these WebForms in my WebApplication i have to extract them out of the assembly (using...
4
by: amit | last post by:
Hi guys!I am trying to write a program which will segregate some selected keywords from a given file.The source code is given alongwith #include<stdio.h> #include<string.h> char...
17
by: Paul | last post by:
On my development computer, I have virtual named host set up, like www.site1.lab. When I upload those to my web site for customer review under mywebsite.com/clients/site1/ it throws some of the...
2
Saghar
by: Saghar | last post by:
Hi, Could somebody help me about this topic? I want to know how we can call and execute a non-java source file in a java application. For example, a simple Java application is run, there is an...
34
by: mr.polik | last post by:
Is it legal to do something like this: #include "x.c" in other source file? And is it good idea? Thank you in advance for your answers, Igor.
5
by: ganesh.kundapur | last post by:
Hi, I want to know how to compile the C source file without using the compiler directly. I mean to say by using compiler componets such as ( cpp, cc, as, ld ). I tried to compile the fallowing...
2
by: DDD | last post by:
I have a web site project using C#. If I release the web site in the VS2005, all works well except that DLL file name has some strange, name APP_Code_erwer234.dll. And erwer234 is a random string....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.