Hello programmers!
I have been working on a hardware store application, where there is a HardwareRecord
class that stores information about each object in a store (ex: nuts, bolts, screws, and so forth).
Here's my declaration of this class:
// HardwareRecord.h
// Initialize Hardware Store File
//
//
// Definition of HardwareRecord class
#ifndef __Initialize_Hardware_Store_File__HardwareRecord__
#define __Initialize_Hardware_Store_File__HardwareRecord__
#include <iostream>
class HardwareRecord
{
public:
HardwareRecord(int=0,std::string="",std::string="",double=0.); //constructor
explicit HardwareRecord();
HardwareRecord operator=(HardwareRecord&);
//'set' and 'get' functions
void setAccountNumber(int);
int getAccountNumber() const;
void setName(std::string);
std::string getName() const;
void setPrice(double);
double getPrice() const;
void setDescription(std::string);
std::string getDescription() const;
void wipeRecord(); //set everything to blank
private:
int myAccountNumber;
std::string myName;
std::string myDescription;
double myPrice;
};
#endif /* defined(__Initialize_Hardware_Store_File__HardwareRecord__) */
As you can see, this class relies on four private members: myAccountNumber
, myName
, myDescription
, and myPrice
. I have declared two constructors- one that sets the values of each of those members, and another that sets each member to default values.
Here's how the class is defined (in a .cpp file):
// Implementation of HardwareRecord class definition
#include <iostream>
#include <string>
#include <cstdlib>
#include "HardwareRecord.h"
using namespace std;
HardwareRecord HardwareRecord::operator=(HardwareRecord & aRecord)
{
this->myAccountNumber=aRecord.myAccountNumber;
this->myName=aRecord.myName;
this->myDescription=aRecord.myDescription;
this->myPrice=aRecord.myPrice;
return *this; //allow for cascaded overloading
}
HardwareRecord::HardwareRecord(int account,string name,string description, double price)
{
setAccountNumber(account);
setName(name);
setPrice(price);
setDescription(description);
}
HardwareRecord::HardwareRecord()
{
setAccountNumber(0);
setName("");
setPrice(0);
setDescription("");
}
void HardwareRecord::wipeRecord()
{
setAccountNumber(0);
setName("");
setPrice(0);
setDescription("");
}
void HardwareRecord::setAccountNumber(int num)
{
if (num < 0)
{
throw invalid_argument("The account number is not in the valid range (greater or equal to 0)");
}
else
{
myAccountNumber=num;
}
}
int HardwareRecord::getAccountNumber() const
{
return myAccountNumber;
}
void HardwareRecord::setName(string name)
{
myName=name;
}
string HardwareRecord::getName() const
{
return myName;
}
void HardwareRecord::setPrice(double price)
{
if (price < 0)
{
throw invalid_argument("The price can not be less than zero");
}
else
{
myPrice=price;
}
}
double HardwareRecord::getPrice() const
{
return myPrice;
}
void HardwareRecord::setDescription(string description)
{
this->myDescription=description;
}
string HardwareRecord::getDescription() const
{
return myDescription;
}
The point of the class is to be used in a random-access file setting, where a .dat file has 100 HardwareRecords (written out in binary) to store information about the store. However, when I try to create a temporary variable: HardwareRecord temp;
, the error pops up: No matching constructor for initialization of 'HardwareRecord'
. Here's how it is used:
// Application that models a store's record of inventory
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <sstream>
#include "HardwareRecord.h" //HardwareRecord definition
using namespace std;
//enumeration of choices
enum Choices {WIPE_RECORDS,UPDATE,LIST,PRINT,DELETE,NEW,END,LAST};
std::ostream& operator<<(std::ostream& op,const Choices& choices)
{
//print the string corresponding to the value of enum type Choices
string output="";
switch (choices)
{
case WIPE_RECORDS:
output = "wipe records";
break;
case UPDATE:
output = "update records";
break;
case LIST:
output = "list records";
break;
case PRINT:
output = "print records";
break;
case DELETE:
output = "delete records";
break;
case NEW:
output = "add new record";
break;
case END:
output = "terminate application";
break;
case LAST:
output = "an option used to iterate over the values in the Choice enumeration";
break;
default:
cerr << "Error. invalid value is read";
exit(EXIT_FAILURE);
break;
}
op << output; //print output
return op;
}
//prototype of helper functions
int enterChoice();
void wipeRecords(fstream&);
void updateRecord(fstream&);
void listRecords(fstream&);
void createTextFile(fstream&);
void deleteRecord(fstream&);
void newRecord(fstream&);
int main()
{
//open file for reading and writinbg
fstream outRecord ("HardwareRecord.dat",ios::in|ios::out|ios::binary);
//exit program if fstream cannot open file
if (!outRecord)
{
cerr << "File could not be opened." << endl;
exit(EXIT_FAILURE);
}
int choice; //user's choice
//enable user to specify action
while ((choice=enterChoice()) !=END)
{
switch (choice)
{
case WIPE_RECORDS: //wipe all records clean
wipeRecords(outRecord);
break;
case UPDATE: //update a record
updateRecord(outRecord);
break;
case LIST: //list all current records
listRecords(outRecord);
break;
case PRINT: //print a record
createTextFile(outRecord);
break;
case DELETE: //delete a record
deleteRecord(outRecord);
break;
case NEW: //add a new record (if space allows)
newRecord(outRecord);
break;
default: //display error if user does not select valid choice
cerr << "Incorrect choice" << endl;
}
outRecord.clear();
}
return 0;
}
//enable user to input menu choice
int enterChoice()
{
//display avaliable options
cout << "\nEnter your choice:\n"<< endl;
Choices aChoice;
for (int c=WIPE_RECORDS; c < LAST; c++)
{
aChoice= (Choices) c;
cout << c << " - " << aChoice << endl;
}
cout << "\n?: ";
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
void wipeRecords(fstream& theFile)
{
HardwareRecord temp; //---THIS IS THE LINE THAT IS CAUSING THE ERROR!!
for (int i=0; i < 100;i++)
{
//convert record from binary and assign to temp
//make temp "wipe itself"
}
}
I realize that a lot of the functions that are defined as prototypes are not given their respective definitions (yet, but I will define them as soon as I can get this problem sorted out).
If you look to the last few lines, you will see the line that is causing the error (I commented it with caps lock for your convenience).
What is going on?
Why am I getting the error?
Thanks in advance!