1 Replies - 2903 Views - Last Post: 18 October 2007 - 10:30 AM Rate Topic: -----

#1 ibleedart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-October 07

Employee Program Inheritence error C2270

Posted 18 October 2007 - 05:52 AM

Hi! Have been working on this one quite awhile. Despite modifications I still have 27 errors! I need to write a program that has an employee and hourly class in which the hourly class inherits from employee. I am coming up with the following errors and can't seem to get them fixed:

employee.h(53) : error C2270: 'getFirstName' : modifiers not allowed on nonmember functions
(how do I use this function then with inheritence?)

employee.h(54) : error C2065: 'firstName' : undeclared identifier
(isn't this declared already or did I do it wrong?)

hourly.h(64) : error C2352: 'employee::setName' : illegal call of non-static member function
(how do I call this then?)

Here is the code:
//Class hourly 

#ifndef H_Hourly
#define H_Hourly

#include "employee.h"	//inheriting from the class employee

#include <iostream>
using namespace std;

class hourly:public employee
{
public:
	void print() const;
	//Function to output the first name, last name and hours worked.
	//Postcondition: Outputs
	//	  firstName  lastName hours worked are 99.99

	double calculateHoursWorked() const;
	//Function to calculate and return the hours worked.
	//Postcondition:  Hours worked is calculated and returned

	void setNameHours(string first, string last, double dayOne, double dayTwo,
		double dayThree, double dayFour, double dayFive);
		//Function to set the first name, last name and hours worked
		//each day according to the parameters.
		//Postcondition:  firstName=first; lastName=last; dayOneHours=dayOne;
		//dayTwoHours=dayTwo; dayThreeHours=dayThree; dayFourHours=dayFour;
		//dayFiveHours=dayFive.

	hourly (string first="", string last="", double dayOne=0, double dayTwo=0,
		double dayThree=0, double dayFour=0, double dayFive=0);
	//Constructor with parameters
	//sets the first name, last name, dayOneHours, dayTwoHours, dayThreeHours,
	//dayFourHours, and dayFiveHours according to the parameters.  If no value is
	//specified the default values are assumed.
	//Postcondition: firstName=first; lastName=last; dayOneHours=dayOne;
	//dayTwoHours=dayTwo; dayThreeHours=dayThree; dayFourHours=dayFour;
	//dayFiveHours=dayFive.

	double dayOneHours;
	double dayTwoHours;
	double dayThreeHours;
	double dayFourHours;
	double dayFiveHours;

private:
};

void hourly::print() const
{
	employee::print(); //print name of employee
	cout<<"'s hours worked are:"<<calculateHoursWorked()<<endl;
}

double hourly::calculateHoursWorked() const
{
	return (dayOneHours+dayTwoHours+dayThreeHours+dayFourHours+dayFiveHours);
}

void employee::setNameHours(string first, string last, double dayOne, double dayTwo,
							double dayThree, double dayFour, double dayFive)
{
	employee::setName(first, last);
	dayOneHours=dayOne;
	dayTwoHours=dayTwo;
	dayThreeHours=dayThree;
	dayFourHours=dayFour;
	dayFiveHours=dayFive;
}

hourly::hourly (string first, string last, double dayOne, double dayTwo, double dayThree
				double dayFour, double dayFive)
				:employee(first, last)
{
	dayOneHours=dayOne;
	dayTwoHours=dayTwo;
	dayThreeHours=dayThree;
	dayFourHours=dayFour;
	dayFiveHours=dayFive;
}

#endif


//Class employee

#ifndef H_Employee
#define H_Employee

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

class employee
{
public:
	void print() const;
	//Function to output the first name and last name in the form of
	//firstName lastName.
	
	void setName(string first, string last);
	//Function to set firstName and lastName according to the parameters.
	//Postcondition: firstName=first; lastName=last

	string getFirstName() const;
	//Function to return the first name.
	//Postcondition: The value of firstName is returned.

	string getLastName() const;
	//Function to return the last name.
	//Postcondition: The value of lastName is returned.

	employee(string first="", string last="");
	//Constructor
	//Sets firstName and lastName according to the parameters.
	//The default values of the parameters are null strings.
	//Postcondition firstName=first; lastName=last;

	string firstName; //variable to store the first name
	string lastName; //variable to store the last name

private:
};

void employee::print() const
{
	cout<<firstName<<" "<<lastName;
}

void employee::setName(string first, string last)
{
	firstName=first;
	lastName=last;
}

string getFirstName() const
{
	return firstName;
}

string getLastName() const
{
	return lastName;
}

employee(string first="", string last="")
{
	firstName=first;
	lastName=last;
}
#endif


main.cpp
#include <iostream>
#include "hourly.h"

using namespace std;

int main()
{
	cout << "This is a program to test if the inheritence for hourly"
		<< " and employee classes work. Now printing sample data: " <<endl;
		
	hourly testEmployee;
	testEmployee.setNameHours(Jane, Smith, 8, 8, 8, 7.5, 6.25);
	cout << "The name and hours of the employee are: ";
	testEmployee.print;

	return 0;
}



If anyone could help with this, I would greatly appreciate it. There are other errors, too, but I need to get by these ones first. Thanks in advance.

-Andrea

Is This A Good Question/Topic? 0
  • +

Replies To: Employee Program Inheritence error C2270

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: Employee Program Inheritence error C2270

Posted 18 October 2007 - 10:30 AM

Ok, lets break this down file by file and get you up and running here. Each set of changes is followed by the updated code. Lets start with the basic class of employee. There are the following changes that need to be made...

employee.h

1) When defining your constructor here in the class, drop the equals on your parameters. You can have the equals on the actual function signature later, but not when defining the method in your class. Remember you are telling the compiler how to build an employee and all it needs to know is that it has a constructor that takes two string variables. The defaults will be set in the implementation of those functions later.

2) Remember when you define a function signature for a method in a class, you must prefix all the function names with the class name it belongs to. You forgot to do this for your functions getFirstName(), getLastName(), employee(). This is telling the compiler that the function belongs to the class employee you have defined above it.

//Class employee

#ifndef H_Employee
#define H_Employee

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

class employee
{
public:
	void print() const;
	//Function to output the first name and last name in the form of
	//firstName lastName.
	
	void setName(string first, string last);
	//Function to set firstName and lastName according to the parameters.
	//Postcondition: firstName=first; lastName=last

	string getFirstName() const;
	//Function to return the first name.
	//Postcondition: The value of firstName is returned.

	string getLastName() const;
	//Function to return the last name.
	//Postcondition: The value of lastName is returned.

	employee(string first, string last);
	//Constructor
	//Sets firstName and lastName according to the parameters.
	//The default values of the parameters are null strings.
	//Postcondition firstName=first; lastName=last;

	string firstName; //variable to store the first name
	string lastName; //variable to store the last name

private:
};

void employee::print() const
{
	cout<<firstName<<" "<<lastName;
}

void employee::setName(string first, string last)
{
	firstName=first;
	lastName=last;
}

// Got to remember to prefix with the class name it belongs to, in this case employee.
string employee::getFirstName() const
{
	return firstName;
}

string employee::getLastName() const
{
	return lastName;
}

// It is ok to then set your default parameters here.
employee::employee(string first="", string last="")
{
	firstName=first;
	lastName=last;
}
#endif




Hourly.h

1) Your function setNameHours() should be prefixed with the class hourly, not employee. It was defined in your hourly class, not in your employee class. So your compiler couldn't find it in your employee class declarations.

2) Remember to access member variables and methods of a class, you use the "this" keyword in conjunction with "->". This is saying "Dereference the current instance and get at its member variables".

3) You were missing a comma on your hourly class constructor after the parameter "dayThree"

//Class hourly

#ifndef H_Hourly
#define H_Hourly

#include "employee.h"	//inheriting from the class employee

#include <iostream>
using namespace std;

class hourly : public employee
{
public:
	void print() const;
	//Function to output the first name, last name and hours worked.
	//Postcondition: Outputs
	//	  firstName  lastName hours worked are 99.99

	double calculateHoursWorked() const;
	//Function to calculate and return the hours worked.
	//Postcondition:  Hours worked is calculated and returned

	void setNameHours(string first, string last, double dayOne, double dayTwo,
		double dayThree, double dayFour, double dayFive);

	//Function to set the first name, last name and hours worked
	//each day according to the parameters.
	//Postcondition:  firstName=first; lastName=last; dayOneHours=dayOne;
	//dayTwoHours=dayTwo; dayThreeHours=dayThree; dayFourHours=dayFour;
	//dayFiveHours=dayFive.

	// Don't set values here, just tell the compiler what types of parameters to expect
	hourly (string first, string last, double dayOne, double dayTwo,
		double dayThree, double dayFour, double dayFive);
	//Constructor with parameters
	//sets the first name, last name, dayOneHours, dayTwoHours, dayThreeHours,
	//dayFourHours, and dayFiveHours according to the parameters.  If no value is
	//specified the default values are assumed.
	//Postcondition: firstName=first; lastName=last; dayOneHours=dayOne;
	//dayTwoHours=dayTwo; dayThreeHours=dayThree; dayFourHours=dayFour;
	//dayFiveHours=dayFive.

	double dayOneHours;
	double dayTwoHours;
	double dayThreeHours;
	double dayFourHours;
	double dayFiveHours;

private:
};

void hourly::print() const
{
	employee::print(); //print name of employee
	cout<<"'s hours worked are:"<<calculateHoursWorked()<<endl;
}

double hourly::calculateHoursWorked() const
{
	return (dayOneHours+dayTwoHours+dayThreeHours+dayFourHours+dayFiveHours);
}

// setNameHours() was defined in hourly class, not employee.
void hourly::setNameHours(string first, string last, double dayOne, double dayTwo,
							double dayThree, double dayFour, double dayFive)
{
	// Use the this keyword to get at the member variables/method for "this" instance
	this->setName(first, last);
	this->dayOneHours=dayOne;
	this->dayTwoHours=dayTwo;
	this->dayThreeHours=dayThree;
	this->dayFourHours=dayFour;
	this->dayFiveHours=dayFive;
}

hourly::hourly (string first, string last, double dayOne, double dayTwo, double dayThree,
				double dayFour, double dayFive)
				:employee(first, last)
{
	// Again using the this keyword to get at this instance
	this->dayOneHours=dayOne;
	this->dayTwoHours=dayTwo;
	this->dayThreeHours=dayThree;
	this->dayFourHours=dayFour;
	this->dayFiveHours=dayFive;
}

#endif




main.cpp

1) You have to define an instance of the class first before you can access the data (unless it is a static method in which case you could use just the class name followed by the double colons... which was generating your static method errors btw because you didn't have static methods)

2) Once you have an instance, you access its methods through this pointer (again using the -> operator) to call its methods and such like print()

3) When you call a method or constructor, make sure you are sending it the right kind of data as a parameter. If you define a function that requires a string (like your hourly class constructor) you must pass it as "Jane" and not simply Jane. Same with the doubles. If you define it as a double, try to pass it doubles. In your case it wasn't flagging the doubles because your integers were being converted to doubles (8 was being changed to 8.0 for instance).

#include <iostream>
#include "hourly.h"

using namespace std;

int main()
{
	cout << "This is a program to test if the inheritence for hourly"
		<< " and employee classes work. Now printing sample data: " <<endl;
		
	// Notice how I define a new instance using the word "new" and pass it values since your constructor asks for them.
	hourly *testEmployee = new hourly("Jane","Smith",8.0,8.0,8.0,7.5,6.25);

	cout << "The name and hours of the employee are: ";

	// Use the instance to call its method "print()"
	testEmployee->print();

	return 0;
}



Now these changes have been tested and compiled and should work for you. Look through the code and any comments I left in it to see what is happening. Hopefully all the changes I made make sense.

Enjoy!

"At DIC we be coding ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1