assigning variables to an array of objects?

I have tried a couple different ways of assigning variables to this array. When I do it this way I get an error on the payroll[i] = h; that says "No viable overloaded '='

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  //
//  main.cpp
//  Week 7 Assignment 1
//
//  Created by Adam Windsor on 10/1/14.
//  Copyright (c) 2014 Adam Windsor. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include "payroll.h"
using namespace std;

const int SIZE = 7;

int main()
{
    Payroll payroll[SIZE];
    
    for (int i = 0; i<1; i++)
    {
        int h;
        double r;
        cout << "Please enter number of hours for employee " << i + 1 << endl;
        cin >> h;
        payroll[i] = h;
        
        
        
    }
    
    
    //cout << Payroll::getHours();
    
    
        return 0;
}




I have also tried this. There is no error message but the variable 'h' is not assigned to the setHours member.

1
2
3
4
5
6
7
8
9
10
11
12
13
Payroll payroll[SIZE];
    
    for (int i = 0; i<1; i++)
    {
        double h;
        double r;
        cout << "Please enter number of hours for employee " << i + 1 << endl;
        cin >> h;
        payroll[i].setHours(h);
        
        
        
    }



Here is the header and .cpp files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
//  payroll.h
//  Week 7 Assignment 1
//
//  Created by Adam Windsor on 10/1/14.
//  Copyright (c) 2014 Adam Windsor. All rights reserved.
//

#ifndef __Week_7_Assignment_1__payroll__
#define __Week_7_Assignment_1__payroll__

#include <stdio.h>
#include <iomanip>

using namespace std;

class Payroll
{
private:
    double Payrate;
    double Hours;
    double GrossPay;
    
public:
    Payroll ();

    void setHours (double h);
    void setPay (double p);
    double getHours ()
    {
        return Hours;
    }
    double getPayrate ()
    {
        return Payrate;
    }
    
    double getGrossPay ()
    {
        return GrossPay;
    }
//    void print()
//    {
//        for (int i =0; i<SIZE; i++)
//        {
//            cout << "The gross pay for employee " << i+1 << " is " <<
//        }
//    }
    
};
#endif /* defined(__Week_7_Assignment_1__payroll__) */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
//  payroll.cpp
//  Week 7 Assignment 1
//
//  Created by Adam Windsor on 10/1/14.
//  Copyright (c) 2014 Adam Windsor. All rights reserved.
//

#include "payroll.h"

Payroll::Payroll()
{
    Payrate = 0;
    Hours = 0;
    
}


void Payroll::setHours(double h)
{
    h=Hours;
}

void Payroll::setPay(double p)
{
    p=Payrate;
}


1
2
3
4
5
6
7
8
9
10
11
void Payroll::setHours(double h)
{
    h=Hours;
    Hours = h;
}

void Payroll::setPay(double p)
{
    p=Payrate;
    Payrate = p;
}
Doing it this way payroll[i].setHours(h); is the correct way in doing so the reason why it doesn't work is there is a problem within your setHours() method.

1
2
3
4
5
6
void Payroll::setHours(double h)
{
    // You are assigning Hours to h when it should be the otherway around.
    // For example it should be Hours = h;
    h=Hours;
}


The same goes for your setPay() method, you need to swap those around also.
Last edited on
Thank you!
Topic archived. No new replies allowed.