Error- request for member ' ' in ' ' which is of non-class type ' '

Dec 8, 2014 at 1:15am
I keep getting the error

"error: request for member 'firstname' in 'object2', which is of non-class type 'employee()'"

I have searched other forums and it appears the problem is when initializing object2. People have said to use
employee object2;

rather than,

employee object2();

but that brings the error,

"error: no matching function for call to 'employee::employee()'"

So I am unsure on what to do now.
Here is the bulk of my code. Left the function definitions out.
I am trying to initialize object2 by using the modifiers.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  using namespace std;

class employee
{
protected:

public:
string firstname, designation;
double salary;
// constructor
    employee (string fname, string desig, double sal);
//accessors
    string get_first();
    string get_designation();
    double get_salary();
//modifiers
    void set_first(string fname);
    void set_designation(string desig);
    void set_salary(double sal);
//overloaders
    void print(char c)
    {
        cout << "printing: " << c << endl;
    }
    void read (char charact)
    {
        cout << "Reading: " << endl;
        cin >> charact;
    }
    void compare(employee obj1, employee obj2)
    {
       if (obj1.salary == obj2.salary)
            cout <<"Employee salaries are the same"<<endl;
       else
            cout <<"Employee salaries are different"<<endl;
    }
    double add(employee obj3, double num1)
    {
        obj3.salary = obj3.salary + num1;
        return obj3.salary;
    }
    string addname(employee obj4, string name1)
    {
        obj4.firstname = obj4.firstname + " " + name1;
    }
};
    employee::employee (string fname, string desig, double sal)
    {
        firstname = fname;
        designation = desig;
        salary = sal;
    }

class consultant: public employee
{
    int tenure;
public:
// constructor
    consultant (int tenure);
// mutators and accessors
    int get_tenure();
    void set_tenture(int tenure);
};
    consultant::consultant (int tenure) : employee(firstname, designation,salary)
    {
    }
int main()
{
    employee object1("Name1","Location1",5000.00);
    employee object2();

    object2.firstname = "Name2";


return 0;
}


Any help is greatly appreciated
Dec 8, 2014 at 1:29am
You haven't provided a default constructor for "employee", which you need if you want to use employee object2;

Defining a default constructor should fix the issue.
1
2
3
4
5
employe::employee()
{
    //do whatever you want with the values
    string = ""; //one example - just an empty string
}
Dec 8, 2014 at 1:45am
Would lines 47-52 not be the default constructor?

1
2
3
4
5
6
employee::employee (string fname, string desig, double sal)
    {
        firstname = fname;
        designation = desig;
        salary = sal;
    }
Dec 8, 2014 at 1:52am
1
2
3
4
5
6
employee::employee (string fname, string desig, double sal)
    {
        firstname = fname;
        designation = desig;
        salary = sal;
    }


We need to provide a constructor that takes no arguments at all, which would look like the one that I give in my other post on this topic.

1
2
3
4
5
employe::employee()
{
    //do whatever you want with the values
    string = ""; //one example - just an empty string
}


If you just do employee object2; with the constructor you are using, your program doesn't know how to set the values for firstname, designation, or salary.
Dec 8, 2014 at 2:22am
Thanks that seems to have worked!

However now I am running into the problem of having the user input the details of another object. I am trying to take the input using the >> operator.

So I declare object 3 like,

employee object3;

Then could I do something like
1
2
 string name;
object3.firstname >> name >> endl; 


Or would I need to create another void function that takes in user input and sets it to each variable found within the object? Or perhaps it be best to modify this?

1
2
3
4
 void employee::set_first(string fname)
{
    firstname = fname;
}






Dec 8, 2014 at 2:27am
Ah, I see what you want to do. Look up operator overloading.

You still have to use cin >> ...

You can do this
cin >> object3.firstname and do that for each of the variables (be careful about the input buffer).

endl is only for output. It won't work for "cin".
Dec 8, 2014 at 2:40am
Okay yeah that makes sense. the prompt I was given only included the ">>" and did not mention cin >> so I figured a separate function would be needed. But that works, so thanks a bunch the help and for the quick reply!
Topic archived. No new replies allowed.