Can't access map within const structure

I am passing a structure into a subroutine as a const reference. One of the elements of the structure is a map. I am trying to access the map with the .find() function. I am getting an error that says something like "passing 'const MyContainer::MapLessThan' as 'this' argument of 'bool MyContainer::MapLessThan::operator()(const MapType&, const MapType&)' discards qualifiers". The code compiles if I change the subroutine parameter to a non-const. Basically, my code looks like this:

bool subroutineA (const MyStructureType myStructure)
{
MapType::const_iterator iter;

iter = myStructure.structMap.find(myKey);
}

Any insight would be greatly appreciated. (Let me know if this is the wrong forum for this question.)
Put all you code and class files up here and I will try to help.
1
2
3
4
5
6
7
8
bool subroutineA (const MyStructureType myStructure)
{
MapType::const_iterator iter = myStructure.structMap.find(myKey);
}

well, actually I don't know about map, but const values should be
initialized while created;
 
I can do that. But, the code is part of a large program. So, I will strip it out and only post the pertinent files. I won't be able to finish until Monday. Thanks.
Here is the code. I trimmed it down to 2 .cc files and 2 .hh files. MyDataLoadWindowCb.cc does not compile. If I remove const from the second parameter of checkForCrossHatching, then it compiles.

MyContainer.hh
#ifndef MyContainer_H
#define MyContainer_H

# include <fstream>
# include <iomanip>
# include <iostream>
# include <sstream>
using namespace std;

#include <map>

class MyContainer
{
public:

enum FieldType
{
STATUS_FIELD_1,
STATUS_FIELD_2,
STATUS_FIELD_3,
STATUS_FIELD_4,
STATUS_FIELD_5,
STATUS_FIELD_6,
STATUS_FIELD_7,
STATUS_FIELD_8,
STATUS_FIELD_9
};


class FieldLessThan
{
public:

bool operator()(const FieldType &left,
const FieldType &right)
{
bool isLessThan = false;

if (left < right)
{
isLessThan = true;
}

return isLessThan;
}
};


struct DataLoadType
{
bool crc;
float dataLoadProgress;
int dataLoadReferenceTime;
int loadStatus;

DataLoadType()
{
crc = false;
dataLoadProgress = 0.0;
dataLoadReferenceTime = 0;
loadStatus = 0;
}
};

typedef map
<FieldType, DataLoadType, FieldLessThan> DataLoadMapType;
typedef map
<FieldType, DataLoadType, FieldLessThan>::value_type DataLoadMapValType;
typedef map
<FieldType, DataLoadType, FieldLessThan>::iterator DataLoadMapIterType;


struct MyStatusDataType
{
DataLoadMapType dataLoadMap;
int myLocation;

MyStatusDataType()
{
myLocation = 0;
}
};

struct MainDataType
{
int myIndex;
MyStatusDataType myStatusData;
};

class MainDataKeyLessThan
{
public:

bool operator()(const int &left,
const int &right)
{
bool isLessThan = false;

if (left < right)
{
isLessThan = true;
}

return isLessThan;
}
};

typedef map
<int, MainDataType, MainDataKeyLessThan> MainDataMapType;
typedef map
<int, MainDataType, MainDataKeyLessThan>::value_type MainDataMapValType;
typedef map
<int, MainDataType, MainDataKeyLessThan>::iterator MainDataMapIterType;


private:

void MyContainer::initializeMyMapsAndVectors
(MyStatusDataType &myStatusData);


MyContainer();
~MyContainer();


//---------------------------------------------------------------------------
// Local Variables
//---------------------------------------------------------------------------

static MyContainer *myContainerInstance;

// The copy constructor and assignment operator are declared
// private and are left unimplemented to ensure that neither is
// automatically generated by the compiler.

MyContainer (const MyContainer &other);

MyContainer& operator=(const MyContainer &other);

MainDataMapType MainDataMap;

public:

bool getMyStatusData
(const int &myIndex,
MyStatusDataType &dataType);

static MyContainer *instance();

}; // end class MyContainer
#endif



MyContainer.cc
#include <iostream>
#include <set>

#include <MyContainer.hh>
#include <MyDataLoadWindowCb.hh>


MyContainer *MyContainer::myContainerInstance = NULL;



//------------------------------------------------------------------------------
//
// NAME: main
//
// NOTES: Just a place holder.
//
//------------------------------------------------------------------------------
int main ()
{
}



bool MyContainer::getMyStatusData
(const int &myIndex,
MyStatusDataType &dataType)
{
MainDataMapIterType MainDataMapIter = MainDataMap.find(myIndex);

if (MainDataMapIter != MainDataMap.end())
{
dataType = MainDataMapIter->second.myStatusData;
return true;
}

return false;
}


void MyContainer::initializeMyMapsAndVectors
(MyStatusDataType &myStatusData)
{
DataLoadType dataLoadData;
FieldType key;

for (int i = STATUS_FIELD_1; i <= STATUS_FIELD_9; i++)
{
key = static_cast<FieldType>(i);

myStatusData.dataLoadMap.insert(DataLoadMapValType(key, dataLoadData));
}
}


MyContainer *MyContainer::instance()
{
if (NULL == myContainerInstance)
{
myContainerInstance = new MyContainer();
}
return myContainerInstance;
}


MyContainer::MyContainer()
{
}

MyContainer::~MyContainer()
{
}


MyDataLoadWindowCb.hh
#ifndef MyDataLoadWindowCb_H
#define MyDataLoadWindowCb_H

#include <map>

#if __HP_aCC
#else
#include <errno.h>
#endif


class MyDataLoadWindowCb
{

public:

static MyDataLoadWindowCb *instance();


bool crossHatchRoutine
(const MyContainer::FieldType statusField,
const int myIndex);


private:

bool checkForCrossHatching
// (const MyContainer::FieldType statusField,
// const HciCommonMyTypes::MyIndexType myIndex);
(const MyContainer::FieldType statusField,
const MyContainer::MyStatusDataType &myData);


MyDataLoadWindowCb();
~MyDataLoadWindowCb();


//---------------------------------------------------------------------------
// Local Variables
//---------------------------------------------------------------------------

static MyDataLoadWindowCb *theInstance;


}; // class MyDataLoadWindowCb
#endif


MyDataLoadWindowCb.cc
#include <string.h>

#include <MyContainer.hh>
#include <MyDataLoadWindowCb.hh>

MyDataLoadWindowCb *MyDataLoadWindowCb::theInstance = NULL;




bool MyDataLoadWindowCb::checkForCrossHatching
(const MyContainer::FieldType statusField,
const MyContainer::MyStatusDataType &myData)
{
float progress;
bool crosshatchField = false;
MyContainer::DataLoadMapType::const_iterator iter;


iter = myData.dataLoadMap.find(statusField);

if (iter != myData.dataLoadMap.end())
{
progress = iter->second.dataLoadProgress;

if (progress > 50.0)
{
crosshatchField = true;
}
}

return crosshatchField;
}


bool MyDataLoadWindowCb::crossHatchRoutine
(const MyContainer::FieldType statusField,
const int myIndex)
{
bool crosshatchField = false;
MyContainer::MyStatusDataType myData;
MyContainer *containerInstance = MyContainer::instance();
bool success;


success = containerInstance->getMyStatusData(myIndex, myData);
if (success)
{
crosshatchField = checkForCrossHatching(statusField, myData);
}
return crosshatchField;
}


MyDataLoadWindowCb::MyDataLoadWindowCb()
{
}
MyDataLoadWindowCb::~MyDataLoadWindowCb()
{
}
Thanks to anyone who was investigating my problem. The answer has been found. I needed to add const to the FieldLessThan operator.


class FieldLessThan
{
public:

bool operator()(const FieldType &left,
const FieldType &right) const // add const here
{
bool isLessThan = false;

if (left < right)
{
isLessThan = true;
}

return isLessThan;
}
};

Topic archived. No new replies allowed.