-2

I try to pass variables to my employee class but I can't see to figure out why it isn't working. I was following along with a tutorial explaining how it works but did cover how to do with already declared variables. So what do take variables initialized in main and pass them employee so I can use function raise to calculate raise later in the code. Im writing this text to padd the word count so I can post it. How much padding does it what.

class Employee
{
private:
        string name;
        int yearjoined;
        double monthlySalary;
    
public:

    Employee()
    {
        name = " ";
        yearjoined = 0;
        monthlySalary = 0;
    }

    void raise(string workerName, int yearJoin, double salary);

    Employee(string workerName, int yearJoin, double salary)
    {
        int yearsworked;
        name = workerName;
        yearjoined = yearJoin;
        monthlySalary = salary;

        cout << name;
        cout << yearjoined;
        cout << monthlySalary;
        /*raise(monthlySalary);*/


    }
    
    void raise(double salary)
        {

        }

};








int main()
{
    
    string userName;
    int userStart;
    double userSalary;


    cout << "What is you name?";
    cin >> userName;
    cout << "What year did you join the company?";
    cin >> userStart;
    cout << "What is you monthly salary?";
    cin >> userSalary;
    Employee worker(string userName, int userStart, double userSalary);
    worker(userName, userStart, userSalary);

}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
Furiouswc
  • 11
  • 1
  • 3
    _"How much padding does it what."_ It doesn't want padding. Nobody wants padding. – Drew Dormann Feb 11 '22 at 19:40
  • when you create the employee object, just use `Employee worker(userName, userStart, userSalary)` This is unecessary: `Employee worker(string userName, int userStart, double userSalary); worker(userName, userStart, userSalary);` – Asphodel Feb 11 '22 at 19:41
  • you're essentially redeclaring variables in the same scope if you do this. – Asphodel Feb 11 '22 at 19:44
  • `Employee worker(string userName, int userStart, double userSalary);` looks to the compiler like a function declaration. – user4581301 Feb 11 '22 at 19:48
  • Learning C++ by tutorials can be really rough. A lot of the information you'll find in internet tutorials is substandard-to-harmful and will slow down your learning. Buying a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to use as a guide costs money, but the time you save should quickly pay itself off. – user4581301 Feb 11 '22 at 19:50

1 Answers1

2

You made one error trying to pass the variables, which made the compiler think you wanted something completely different.

This line:

Employee worker(string userName, int userStart, double userSalary);

The compiler understands it as deceleration of a function named worker that receives 3 arguments and returns an object of class Employee.

This is because you wrote types and variable names in the brackets.

Next, this line:

worker(userName, userStart, userSalary);

The compiler now thinks you want to call the worker function you declared in the previous line (because you wrote nothing before worker), but when it comes the time to link your code (put it together with all functions it needs), there is no function worker defined anywhere, so you get an error.

What you need to do, is combine those 2 line in one like this:

Employee worker(userName, userStart, userSalary);

Now you are telling the compiler to create a variable of type Employee and initialize it with 3 values using the constructor you wrote.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Lev M.
  • 6,088
  • 1
  • 10
  • 23