Const in Class Method Definition

This is a discussion on Const in Class Method Definition within the C++ Programming forums, part of the General Programming Boards category; Can someone please explain to me why I'm getting the following compiler errors when I compile this code. I've been ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Const in Class Method Definition

    Can someone please explain to me why I'm getting the following compiler errors when I compile this code. I've been reading a C++ book, and it tells me that I should always use const at the end of the definition (int function() const which apparently says that the function I'm using it with won't be changing the values of variables, and that using const is a good way to help ensure bug-free programs.

    These are the errors:

    Code:
    Error: int Employee::getAge(void)' : overloaded member function not found in 'Employee'	
    Error: int Employee::getYearOfService(void)' : overloaded member function not found in 'Employee'
    Error: int Employee::Salary(void)' : overloaded member function not found in 'Employee'
    And the code here:

    Code:
    #include <iostream>
     
    using namespace std;
     
    class Employee {
    public:
    	Employee();
    	~Employee();
    	int getAge() const;
    	void setAge(int Age);
    	int getYearOfService() const;
    	void setYearOfService(int YearOfService);
    	int getSalary() const;
    	void setSalary(int Salary);
    private:
    	int itsAge;
    	int itsYearOfService;
    	int itsSalary;
    };
     
    Employee::Employee(){
    	itsAge = 0;
    	itsYearOfService = 0;
    	itsSalary = 0;
    }
     
    Employee::~Employee(){
     
    }
     
     
    int Employee::getAge(){
    	return this->itsAge;
    }
     
    void Employee::setAge(int Age){
    	this->itsAge = Age;
    }
     
    int Employee::getYearOfService(){
    	return this->itsYearOfService;
    }
     
    void Employee::setYearOfService(int YearOfService){
    	this->itsYearOfService = YearOfService;
    }
     
    int Employee::getSalary(){
    	return this->itsSalary;
    }
     
    void Employee::setSalary(int Salary){
    	this->itsSalary = Salary;
    }
     
    int main(){
    	Employee Employee1;
    	Employee Employee2;
    	Employee1.setAge(21);
    	Employee1.setSalary(26000);
    	Employee1.setYearOfService(6);
    	Employee2.setAge(33);
    	Employee2.setSalary(39000);
    	Employee2.setYearOfService(13);
    	cout << "Employee 1 - Age: " << Employee1.getAge() << endl;
    	cout << "Employee 1 - Salary: " << Employee1.getSalary() << endl;
    	cout << "Employee 1 - Years Served: " << Employee1.getYearOfService() << endl << endl;
    	cout << "Employee 2 - Age: " << Employee2.getAge() << endl;
    	cout << "Employee 2 - Salary: " << Employee2.getSalary() << endl;
    	cout << "Employee 2 - Years Served: " << Employee2.getYearOfService() << endl;
    	cin.get();
    }
    Thanks a lot!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You inadvertently put the solution in the thread title.
    Code:
    class Employee {
    public:
    	int getYearOfService() const;
    Note that that's a class method (or member function in typical C++ terminology) declaration, not a definition. The definition is here:
    Code:
    int Employee::getYearOfService(){
    	return this->itsYearOfService;
    }
    and the problem is that there is no const there. It should be
    Code:
    int Employee::getYearOfService() const {
    	return this->itsYearOfService;
    }
    You can have two functions with the same name and parameters, but different constness.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Ah I see now! Thanks a lot for clearing that up for me!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Problems: Operator overloading.
    By Dual-Catfish in forum C++ Programming
    Replies: 17
    Last Post: 06-18-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21