0

So I am trying to read in a file using private class variables. I am unsure how to display the file. There might be another way to do this, but this is what I could think of. Note, its my first project using classes and private and public/private members. Was I on the right path atleast? I keep getting an error for the int main function. How can I fix it? This is my main:

#include "Record.h"
#include <sstream>

int main ()
{   
    Record employee;

    ifstream myFile;
     myFile.open("Project 3.dat");
    string str;
    int i=0;

    if (myFile.is_open())
    {
        while (getline(myFile, str))
        {
            istringstream ss(str);
           ss >> employee.get_name(str) >> employee.get_id(stoi(str)) >> 
   employee.get_rate(stoi(str)) >> employee.get_hoursWorked(stoi(str));
        }
    }

    return 0;
}

This is my header:

#include <iostream>
#include <fstream>
#include <string> 
using namespace std;

class Record
{
    private:
        string name;
        int id;
        double rate;
        double hours;
    public: 
        Record();
        Record (string n, int empid, double hourlyRate, double hoursWorked); 
 // constructor

        void read_data_from_file();
        double calculate_wage();
        void print_data();

        /* ASETTERS AND GETTERS */          
        void set_name (string n);
        string get_name();

        void set_id (int empid);
        int get_id();

        void set_rate (double hourlyRate);
        double get_rate();

        void set_hoursWorked(double hoursWorked);
        double get_hoursWorked();
        /* END OF SETTERS AND GETTERS */            
};

This is my cpp

#include "Record.h"
Record::Record():name(), id(0), rate(0), hours(0) {} //default constructor 
must be implemented first
Record::Record(string n, int empid, double hourlyRate, double hoursWorked)
{
    name = n;
    empid = id;
    hourlyRate = rate;
    hoursWorked = hours;
}
//
void Record::set_name(string n)
{
    name = n;
}
string Record::get_name()
{
    return name;
}
//
void Record::set_id(int empid)
{
    id = empid;
}
int Record::get_id()
{
    return id;
}
//
void Record::set_rate(double hourlyRate)
{
    rate = hourlyRate;
}
double Record::get_rate()
{
    return rate;
}
//
void Record::set_hoursWorked(double hoursWorked)
{
    hours = hoursWorked;
}
double Record::get_hoursWorked()
{
    return hours;
}
//


double Record::calculate_wage()
{
    return (rate * hours);
}
  • This question would really benefit from a [mcve], including the actual compiler error. As it is, there's so much code that's unnecessary for reproducing the error, making it harder for people to find and fix the error, as well as harder for future visitors with the same problem. – chris Feb 08 '18 at 02:30
  • line 18 for main func [Error] no matching function for call to 'Record::get_name(std::string&)' –  Feb 08 '18 at 02:32
  • and [Error] 'stoi' was not declared in this scope but I included sstream library –  Feb 08 '18 at 02:32
  • are you calling the wrong function? You declared `string get_name();` without parameters but you called `employee.get_name(str)` and have a `str` (string) parameter, I thought it should be the **setter** - `employee.set_name(str)` right? – JaxLee Feb 08 '18 at 02:36
  • ^ that gives me an error too. 18 [Error] no match for 'operator>>' (operand types are 'std::istringstream {aka std::basic_istringstream}' and 'void') and stoi was not declared in scope, again –  Feb 08 '18 at 02:38

1 Answers1

0

There are some issues with your code that I can see. most of your problems aren't related to your question (I mean using a class or private/public members). you have more basic misunderstandings. So here's some explanation that might help you:

1- Using functions : You have some troubles using your defined functions, A function can have multiple input parameters and one return value. basically it's like this return_type function_name(parameter_type param1, ...). it means that if you call this function you need to pass param1,... and expect your function operation and then have a return value of return_type. You defined some set and get functions. if you want to set something you should call set function and pass your desired value to it and it will copy your value to your defined member data, after that you can call get function to retrieve that value. So when you call get function with parameter it will raise error. Here you want to call set function.

2- Using stoi : As you can see you are getting error on using stoi function too, this is a function for converting string to integer, The thing that you missed here is that this function declared in std namespace. If you want to use it you need to use it like this std::stoi(str). one other thing, using namespace std is a bad practice.

3- Design matters : In OOP design, a class must have a purpose and an actual job to do. It might be an interface to abstract class but a bunch of set and get functions will not fulfill the need to create a class. Here if your class is going to do file operations, it's OK, but as far as you shared your code it's just some set and get functions.

HMD
  • 2,202
  • 6
  • 24
  • 37