M
Mike Copeland
I'm trying to learn and adopt some new programming techniques, and
I've reached a roadblock. Specifically, although I have most of a class
defined and declared, I don't know how to use it.
In the code below I have functions that populate the elements of a
class object, but I don't know how to (1) store the object in the
declared map and (2) access/update the information in the objects that
are stored.
My intent here is to have one or more objects stored in the map, but
to instantiate each and use information from one for a cycle of
activity, followed by placing it back into the map. These activities
will repeat off and on throughout the program's execution, and I need to
reacquire some of the objects (one at a time) to use and update the
information that's current in the object.
So, I need to initially instantiate each object as needed, use and
modify its data values, save the object back into the map, use another
map object, and (possibly) reuse any of the objects I've processed. My
quandry is how to for each object initially store, access/update it as
needed, restore it and build/use another object, etc.
The following code doesn't compile, telling me I'm trying to use my
declarations incorrectly:
pfWork.setFileName(printFile);// store new print file info object
pfWork.setIsOpen(true), pfWork.clrRecCount();
print_map[cRCode] = pfWork; // cRCode is a character variable
The class declarations are:
class PrintFile // Print File class
{
bool bIsOpen; // private members
bool bIsUsed;
int nRecords;
string strFileName;
public:
PrintFile(const string sFileName) : // constructor
bIsOpen(true), bIsUsed(true), nRecords(0),
strFileName(sFileName) {};
PrintFile() : // default constructor
bIsOpen(false), bIsUsed(false), nRecords(0),
strFileName("") {};
PrintFile(const PrintFile &pf) : // copy constructor
bIsOpen(pf.bIsOpen), bIsUsed(pf.bIsUsed),
nRecords(pf.nRecords),
strFileName(pf.strFileName) {};
PrintFile &operator= (const PrintFile &rhs) // operator =
{
if(this == &rhs) return *this; // don't assign to self
bIsOpen = rhs.bIsOpen;
bIsUsed = rhs.bIsUsed;
nRecords = rhs.nRecords;
strFileName = rhs.strFileName;
return *this;
};
// access private members
bool getOpened () const { return bIsOpen; };
bool getUsed () const { return bIsUsed; };
int getRecords () const { return nRecords; };
string getFileName () const { return strFileName; };
void setFileName (const string sfn) { strFileName = sfn; };
void setIsOpen (const bool bState) { bIsOpen = bState; };
void setIsUsed (const bool bState) { bIsUsed = bState; };
void addRecCount () { nRecords++; };
void clrRecCount () { nRecords = 0; };
}; // end of class PrintFile
typedef map<char, PrintFile> print_map;
print_map printFiles; // 1 object per event
PrintFile pfWork; // working print file object
I've reached a roadblock. Specifically, although I have most of a class
defined and declared, I don't know how to use it.
In the code below I have functions that populate the elements of a
class object, but I don't know how to (1) store the object in the
declared map and (2) access/update the information in the objects that
are stored.
My intent here is to have one or more objects stored in the map, but
to instantiate each and use information from one for a cycle of
activity, followed by placing it back into the map. These activities
will repeat off and on throughout the program's execution, and I need to
reacquire some of the objects (one at a time) to use and update the
information that's current in the object.
So, I need to initially instantiate each object as needed, use and
modify its data values, save the object back into the map, use another
map object, and (possibly) reuse any of the objects I've processed. My
quandry is how to for each object initially store, access/update it as
needed, restore it and build/use another object, etc.
The following code doesn't compile, telling me I'm trying to use my
declarations incorrectly:
pfWork.setFileName(printFile);// store new print file info object
pfWork.setIsOpen(true), pfWork.clrRecCount();
print_map[cRCode] = pfWork; // cRCode is a character variable
The class declarations are:
class PrintFile // Print File class
{
bool bIsOpen; // private members
bool bIsUsed;
int nRecords;
string strFileName;
public:
PrintFile(const string sFileName) : // constructor
bIsOpen(true), bIsUsed(true), nRecords(0),
strFileName(sFileName) {};
PrintFile() : // default constructor
bIsOpen(false), bIsUsed(false), nRecords(0),
strFileName("") {};
PrintFile(const PrintFile &pf) : // copy constructor
bIsOpen(pf.bIsOpen), bIsUsed(pf.bIsUsed),
nRecords(pf.nRecords),
strFileName(pf.strFileName) {};
PrintFile &operator= (const PrintFile &rhs) // operator =
{
if(this == &rhs) return *this; // don't assign to self
bIsOpen = rhs.bIsOpen;
bIsUsed = rhs.bIsUsed;
nRecords = rhs.nRecords;
strFileName = rhs.strFileName;
return *this;
};
// access private members
bool getOpened () const { return bIsOpen; };
bool getUsed () const { return bIsUsed; };
int getRecords () const { return nRecords; };
string getFileName () const { return strFileName; };
void setFileName (const string sfn) { strFileName = sfn; };
void setIsOpen (const bool bState) { bIsOpen = bState; };
void setIsUsed (const bool bState) { bIsUsed = bState; };
void addRecCount () { nRecords++; };
void clrRecCount () { nRecords = 0; };
}; // end of class PrintFile
typedef map<char, PrintFile> print_map;
print_map printFiles; // 1 object per event
PrintFile pfWork; // working print file object