Hello Everyone,

I just started to program and I'm having a bit difficult with it.
Can someone run this code and please explain to me these errors and why they are not compiling.
Error 1 error C2228: left of '.setFirstName' must have class/struct/union c:\documents and settings\administrator\my documents\visual studio 2008\projects\projec314p\projec314p\project314p.cpp 71
Error 2 error C2228: left of '.displayMessage' must have class/struct/union c:\documents and settings\administrator\my documents\visual studio 2008\projects\projec314p\projec314p\project314p.cpp 72

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

//Employee class
class Employee
{
public:
	//constructor
	Employee(string fname, string lname)
	
	{
		setFirstName(fname);
		setLastName(lname);
	}
	
	void setFirstName(string fname)
	
	{
		FirstName = fname;
	}
	
	string getFirstName()
	
	{
		return FirstName;
	}
	
	void setLastName(string lname)
	
	{
		LastName = lname;
	}
	
	string getLastName()
	
	{
		return LastName;
	}

	void displayMessage()
	{
		cout << "Please enter your first name " << getFirstName() << endl;
		cout << "Please enter your last name " << getLastName() << endl;
	};
private:
	string FirstName;
	string LastName;
	int salary;
};

	int main()
	
	{
	
		string firstname;
		string lastname;
		string salary;
		int salary1;
		
		Employee myEmployee1();
		Employee myEmployee2();
	
		
		

		cout << "Your first name is " << endl;
		myEmployee1.setFirstName(firstname);
		myEmployee1.displayMessage();
		
		cout << "Your last name is " << endl;
		myEmployee2.setLastName(lastname);
		myEmployee2.displayMessage();
		
		cout << "You make how much an hour? " << getline(cin, salary)<< endl;

		salary1 = salary * 40 * 4;
		
		if (salary % 2) 
			salary = salary1 * .1;
			cout <<"Your monthly earnings are " << salary << endl;
		if (salary % 1)
			salary = salary1 * 0;
			cout <<"Your monthly earnings are " << salary << " too bad" << endl;


		return 0;
	}

Recommended Answers

All 2 Replies

when you call this line: Employee myEmployee1(); you're trying to create a class by calling the constructor with no parameters. The problem is that you didn't define a constructor with 0 parameters. The only constructor you created is one with 2 parameters: Employee(string fname, string lname) . So to create a class you need to call it by passing 2 strings to the constructor: Employee myEmployee1("unknown","unknown"); Your next problem will be on these lines:

cout << "You make how much an hour? " << getline(cin, salary)<< endl;

    salary1 = salary * 40 * 4;

salary is a string, and salaray1 is an int. You can't multiply it that way. So change salary to type int and then change the first line to :

cout << "You make how much an hour? ";
cin >> salary;

u have to add a default constructor in the class to continue with your code. Default constructor is created by default if u don't declare any constructor but if u declare some constructor then its not done.

add the following in the Employee class

Employee(){}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.