0

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.

0

Out of curiosity what happens if you define your constructor in the class in the header file instead of the .cpp file?

0

I found out what the problem was. It was not with the compiler, as I may have previously thought.

As you may know about random access file programs, a separate program is required to make a .dat file, so I had two projects for it: one to create the .dat file with special specifications, and another one to actually act as the application.

I created the HardwareRecord.h and the HardwareRecord.cpp file, and I added them to both projects as they were needed.
However, when I was working on the HardwareRecord application project (after I finished the project that initializes the file), I felt that certain changes were neccessary, and thus, I altered the classes' contents. This resulted in two versions of the HardwareRecord class which were conflicting with each other- hence the error message raised when it had one definition of initializer and expected another initializer.

I would like to express my sincere thanks to those that have tried to help me out, Moschops, NathanOliver and DavidW. While You may not have been able to help me out completely, you helped me clean up my code in ways that I would not have even imagined.

I wish you all the best, and for those that are looking at this thread- please learn from my lesson. It will serve you well.

Edited by nathan.pavlovsky: pressed enter too early

0

I would also like to apoligize for asking people spending their precious time on my problem, and for any inconvenience I may have created.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.