Here's an update on the situation.
I thought that maybe initializing the temp with a different method would fix the problem, so I put instead of HardwareRecord temp;
HardwareRecord temp(0,"","",0);
, and this also did not compile.
This means my compiler is screwing up my constructors, and I do not know what is wrong.
Here is my code right now:
//
// HardwareRecord.h
// Initialize Hardware Store File
//
// Created by --------- on 5/18/14.
// Copyright (c) 2014 --------. All rights reserved.
//
// Definition of HardwareRecord class
#ifndef __Initialize_Hardware_Store_File__HardwareRecord__
#define __Initialize_Hardware_Store_File__HardwareRecord__
#include <iostream>
class HardwareRecord
{
public:
HardwareRecord(const unsigned& account=0,const std::string& name="",const std::string& description="",const double& price=0);
HardwareRecord operator=(const HardwareRecord&);
//'set' and 'get' functions
void setAccountNumber(unsigned);
unsigned 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:
unsigned myAccountNumber;
std::string myName;
std::string myDescription;
double myPrice;
};
#endif /* defined(__Initialize_Hardware_Store_File__HardwareRecord__) */
My .cpp file for the class definition:
//
// HardwareRecord.cpp
// Initialize Hardware Store File
//
// Created by --------------- on 5/18/14.
// Copyright (c) 2014 ---------. All rights reserved.
//
// Implementation of HardwareRecord class definition
#include <iostream>
#include <stdexcept> //std::invalid_argument
#include "HardwareRecord.h"
using namespace std;
using std::string;
using std::invalid_argument;
HardwareRecord HardwareRecord::operator=(const 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(const unsigned& account,const string& name,const string& description,const double& price):myAccountNumber(account),myName(name),myDescription(description),myPrice(price)
{}
void HardwareRecord::wipeRecord()
{
setAccountNumber(0);
setName("");
setPrice(0);
setDescription("");
}
void HardwareRecord::setAccountNumber(unsigned num)
{
myAccountNumber=num;
}
unsigned 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("\nThe 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;
}
And finally, my main.cpp file, where I still get the error:
//
// main.cpp
// HardwareStoreApplication
//
// Created by ---------------- on 6/17/14.
// Copyright (c) 2014 ------- --------. All rights reserved.
//
// 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};
const string SELECT[]= {"Wipe Records","Update Records","List Records","Print Records","Delete Records","Add New Record","End Program"};
std::ostream& operator<<(std::ostream& op,const Choices& choice)
{
//print the string corresponding to the value of enum type Choices
op << SELECT[choice]; //print output
return op;
}
//prototype of helper functions
unsigned enterChoice();
void wipeRecords(/*fstream&*/); //can do later
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)
{
int choice; //user's choice
do //enable user to specify action
{
//ONLY valid choices get returned
choice=enterChoice();
cout << "\nYour choice was to " << SELECT[choice] << " ... \n";
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;
case END:
cout << "\nPress 'Enter' to continue/exit... " << flush;
cin.get();
break;
}
outRecord.clear();
} while (choice != END);
}
else //exit program if fstream cannot open file
{
cerr << "\nFile could not be opened." << endl;
exit(EXIT_FAILURE);
}
return 0;
}
//enable user to input menu choice from menu options
//now ... loops until there is a valid input. //
unsigned enterChoice()
{
unsigned menuChoice;
//loop until have a vailid input
for (;;) //example of a C/C++ 'forever' loop ... //
{
menuChoice = LAST;
//display avaliable options
cout << "\nEnter your choice: \n" << endl;
for (unsigned i=WIPE_RECORDS; i < LAST; i++)
{
cout << i << " - " << SELECT[i] << endl;
}
cout << "\nEnter your choice > ";
//'CRASH PROOF' & make sure only valid numbers accepted //
if (cin >> menuChoice && cin.get() == '\n')
{
if (menuChoice < LAST)
break; //break out of loop RIGHT NOW...//
}
//else ... when reach here ...
cin.clear(); //clear any cin error flags ...
cin.sync(); //'flush' cin
cout << "\nOnly integers in range 0..." << LAST-1 << " are valid here.\n";
cout << "Please try again... \n";
}
return menuChoice;
}
//Can do this later ... //
void wipeRecords(fstream& theFile)
{
HardwareRecord temp(0,"","",0);
for (int i=0; i < 100;i++)
{
//convert record from binary and assign to temp
//make temp "wipe itself"
/*theFile.seekg(i*sizeof(HardwareRecord)); //move to position
theFile.read(reinterpret_cast<char*>(&temp),sizeof(HardwareRecord));*/
}
}
As you can see, I tried to initialize temp
with the default values for the only constructor that is defined in the class, and the I got same error that I got for the line HardwareRecord temp;
.
Something is going wrong. I believe that this may be a compiler issue, since I defined everything properly.
Please respond ASAP about this.