Getting an error on a line, but I cannot see what is wrong

Write your question here.
I am trying to compile a file employee.cpp
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
  Put the code you need help with here.
//This is the header file employee.h
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
using namespace std;

namespace employeessavitch
{
	class Employee
	{
	public:
		Employee( );
		Employee(string the_name, string the_ssn);
		string get_name( ) const;
		string get_ssn( ) const;
		double get_net_pay( ) const;
		void set_name(string new_name);
		void set_ssn(string new_ssn);
		void set_net_pay(double new_net_pay);
		void print_check( ) const;
	private:
		string name;
		string ssn;
		double net_pay;
	};
}   //employeessavitch

#endif     //EMPLOYEE_H 


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
//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h
#include<string>
#include<cstdlib>
#include<iostream>
#include "Employee.h"
using namespace std;

namespace employeessavitch
{
	Employee::Employee( ) : name("no name yet"), ssn("No number yet"), net_pay(0)
	{
		//deliberately empty
	}
	
	Employee::Employee(string the_name, string the_number) 
		: name(the_name), ssn(the_number), net_pay(0)
	{
		//deliberately empty
	}
	
	string Employee::get_name() const
	{
		return name;
	}
	
	string Employee::get_ssn( ) const
	{
		return ssn;
	 } 
	 
	 double Employee::get_net_pay( ) const
	 {
	 	return net_pay;
	 }
	 
	 void Employee::set_name(string new_name)
	 {
	 	name = new_name;
	 }
	 void Employee::set_ssn(string new_ssn)
	 {
	 	ssn = new_ssn;
	 }
	 void Employee::set_net_pay (double new_net_pay)
	 {
	 	net_pay = new_net_pay;
	 }
	 
	 void Employee::print_check( ) const
	 {
	 	cout << "\nERROR: print_check FUNCTION CALLED FOR AN \n"
	 	     << "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
	 	     << "Check with the author of the program about this bug.\n";
	 	exit(1);
	   }  
} //employeessavitch 

I am getting an error on the line:
void Employee::set_net_pay (double new_net_pay)
but I cannot see why I am getting this error.
This is error that I get:
46 49 C:\Dev-Cpp\SavitchChapter14\employee.cpp [Error] no 'void employeessavitch::Employee::set_net_pay(double)' member function declared in class 'employeessavitch::Employee'
I can't reproduce your error.
Could you please add the code where you use your class?
Hello Bopaki,

I loaded your program in VS 2015 and could not duplicate the error that you get. What I did notice is:

Creating a namespace in the header file is fine,but you do not need to duplicate this in the .cpp file.

no 'void employeessavitch::Employee::set_net_pay(double) . The answer is right in front of you. What you do not put in your code is done at compile time. Same thing that happens when you use using namespace std;.

In the .cpp file on line 10 try using namespace employeessavitch; and comment or remove the {} on lines 11 and 58 and see what happens.

My experience has been that you declare the namespace in the header file and qualify the functions in the .cpp file just like I qualify things with "std::" only using the namespace that I created. then in other files all I do is include the header fie with my namespace and use that name to qualify the functions that I use.

One other thought is that you compiler may not be using the C++11 or greater standard and that is why you are getting an error.

Hope that helps,

Andy
Thank you all.
My program compiled OKAY this morning. I did nothing to it.
I just opened it and compiled it and it was fine.
Now the question would be "Why was I getting that error yesterday.
I still get problems when I have a header file, implementation file and the main (Driver file)
When I compile the main(Driver file I get message that have lots of "undefined reference to employee.cpp".
When I put all the three files(the header file, the implementation file & the main(Driver file) in one big file, the program runs fine.
What compiler / IDE do you use? Easiest is if all files are in the same directory.
The .cpp need to be included in the project.
I use Dev-cpp C++ 5.9.2
When you create a new project it has a main.cpp already.
To add a new file you go to Project->New File and it gets added to the project.
To include a header file just add #include "FileName"

In your Employee project you need to include "Employee.h" in in main.cpp and in employee.cpp.

BTW The lastest version of Dev-cpp is now 5.11. http://orwelldevcpp.blogspot.co.uk/
Last edited on
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
//This is the header file employee.h
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
using namespace std;

namespace employeessavitch
{
	class Employee
	{
	public:
		Employee( );
		Employee(string the_name, string the_ssn);
		string get_name( ) const;
		string get_ssn( ) const;
		double get_net_pay( ) const;
		void set_name(string new_name);
		void set_ssn(string new_ssn);
		void set_net_pay(double new_net_pay);
		void print_check( ) const;
	private:
		string name;
		string ssn;
		double net_pay;
	};
}   //employeessavitch

#endif     //EMPLOYEE_H 

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
#ifndef HOURLYEMPLOYEE_H
#define HOURLYEMPLOYEE_H

#include<string>
#include "employee.h"

using namespace std;

namespace employeessavitch
{
	class HourlyEmployee : public Employee
	{
	public:
		HourlyEmployee( );
		HourlyEmployee(string the_name, string the_ssn,
						double the_wage_rate, double the_hours);
		void set_rate(double new_wage_rate);
		double get_rate( ) const;
		void set_hours(double hours_worked);
		double get_hours( ) const;
		void print_check( ) ;  //You only list the declaration of an inherited member function
		                       //if you want to change the definition of the function
	private:
		double wage_rate;
		double hours;
	};
}   //employeessavitch
#endif     //HOURLYEMPLOYEE_H 

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
//This is the header file salariedemployee.h
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H

#include <string>
#include "employee.h"

using namespace std;

namespace employeessavitch
{
	class SalariedEmployee : public Employee
	{
	public:
		SalariedEmployee( );
		SalariedEmployee (string the_name, string the_ssn,
							double the_weekly_salary);
		double get_salary( ) const;
		void set_salary(double new_salary);
		void print_check( );
	private:
		double salary;    //weekly
	};
}  //employeessavitch
#endif    //SALARIEDEMPLOYEE_H 

The cpp files will follow:
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
//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h
#include<string>
#include<cstdlib>
#include<iostream>
#include "Employee.h"
using namespace std;

namespace employeessavitch
{
	Employee::Employee( ) : name("no name yet"), ssn("No number yet"), net_pay(0)
	{
		//deliberately empty
	}
	
	Employee::Employee(string the_name, string the_number) 
		: name(the_name), ssn(the_number), net_pay(0)
	{
		//deliberately empty
	}
	
	string Employee::get_name() const
	{
		return name;
	}
	
	string Employee::get_ssn( ) const
	{
		return ssn;
	 } 
	 
	 double Employee::get_net_pay( ) const
	 {
	 	return net_pay;
	 }
	 
	 void Employee::set_name(string new_name)
	 {
	 	name = new_name;
	 }
	 void Employee::set_ssn(string new_ssn)
	 {
	 	ssn = new_ssn;
	 }
	 void Employee::set_net_pay (double new_net_pay)
	 {
	 	net_pay = new_net_pay;
	 }
	 
	 void Employee::print_check( ) const
	 {
	 	cout << "\nERROR: print_check FUNCTION CALLED FOR AN \n"
	 	     << "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
	 	     << "Check with the author of the program about this bug.\n";
	 	exit(1);
	   }  
} //employeessavitch 

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
//This is the file: hourlyemployee.cpp
//This is  the implementation for the class HourlyEmployee
//The interface for the class HourlyEmployee is in
//the header file hourlyemployee.h
#include <string>
#include <iostream>
#include "hourlyemployee.h"
using namespace std;

namespace employeessavitch
{
	HourlyEmployee::HourlyEmployee( ) : Employee( ), wage_rate(0), hours(0)
	{
		//deliberately empty
	}
	
	HourlyEmployee::HourlyEmployee(string the_name, string the_number,
								double the_wage_rate, double the_hours)
	: Employee(the_name, the_number), wage_rate(the_wage_rate), hours(the_hours)
	{
		//deliberately empty
	}
	
	void HourlyEmployee::set_rate(double new_wage_rate)
	{
		wage_rate = new_wage_rate;
	}
	
	double HourlyEmployee::get_rate( ) const
	{
		return wage_rate;
	 } 
	 
	 void HourlyEmployee::set_hours(double hours_worked)
	 {
	 	hours = hours_worked;
	 }
	 
	 double HourlyEmployee::get_hours() const
	 {
	 	return hours;
	 }
	 
	 void HourlyEmployee::print_check()
	 {
	 	set_net_pay(hours * wage_rate);
	 	
	 	cout<< "\n________________________________________________________\n";
	 	cout<< "Pay to the order of " << get_name( ) << endl;
	 	cout<< "The sum of " << get_net_pay( ) << " Dollars\n";
	 	cout<< "____________________________________________________________\n";
	 	cout<< "Check Stub: NOT NEGOTIABLE\n";
	 	cout<< "Employee Number: " << get_ssn( ) << endl;
	 	cout<< "Hourly Employee. \nHours worked: " << hours
	 	    << " Rate: " << wage_rate << " Pay: " << get_net_pay( ) << endl;
	 	cout<< "______________________________________________________________\n";
	 }
}   //employeessavitch 

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
//This is the file salariedemployee.cpp
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the heade fil/e salariedemployee.h
#include<iostream>
#include<string>
#include "salariedemployee.h"
using namespace std;

namespace employeessavitch
{
	SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
	{
		//deliberately empty
	}
	
	SalariedEmployee::SalariedEmployee(string the_name, string the_number,
								double the_weekly_salary)
					: Employee(the_name, the_number), salary(the_weekly_salary)
	{
		//deliberately empty
	}
	
	double SalariedEmployee::get_salary( ) const
	{
		return salary;
	 } 
	 
	 void SalariedEmployee::set_salary(double new_salary)
	 {
	 	salary = new_salary;
	 }
	// void SalariedEmployee::print_check( )
	 //{
	 //	set_net_pay(salary);
	 //	cout<< "\n________________________________________________________________\n";
	 //	cout<< "Pay to the order of " << get_name( ) << endl;
	 //	cout<< "The sum of " << get_net_pay( ) << " Dollars\n" ;
	 //	cout<< "____________________________________________________________________\n";
	 //	cout<< "Check Stub NOT NEGOTIABLE \n";
	 	
	void SalariedEmployee::print_check()
	{
		set_net_pay(salary);
		cout<< "\n__________________________________________________________________\n";
		cout<< "Pay to the order of " << get_name( ) << endl;
		cout<< "The sum of " << get_net_pay( ) << " Dollars\n" ;
		cout<< "_____________________________________________________________________\n";
		cout<< "Check Stub NOT NEGOTIABLE \n";
		cout<< "Employee Number: " << get_ssn( ) << endl;
		cout<< "Salaried Employee. Regular Pay: "
		    << salary << endl;
		cout<< "_____________________________________________________________________\n";
	}
}  //employeessavitch 

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
#include<iostream>
#include "hourlyemployee.h"
#include "salariedemployee.h"
using std::cout;
using std::endl;
using namespace employeessavitch;

int main()
{
	HourlyEmployee joe;
	joe.set_name("Might Joe") ;
	joe.set_ssn("12345-6789" ) ;
	joe.set_rate(20.50) ;
	joe.set_hours(40);
	cout << "Check for " << joe.get_name( )
	     << " for " << joe.get_hours( ) << " hours.\n";
	joe.print_check( );
	cout << endl;
	
	SalariedEmployee boss("Mr. Big Shot", "987-65-4321", 10500.50);
	cout << "Check for " << boss.get_name( ) << endl;
	boss.print_check( );
	
	return 0;   
}

When I compile I get this warning message:

C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x64): undefined reference to `employeessavitch::HourlyEmployee::HourlyEmployee()'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0xad): undefined reference to `employeessavitch::Employee::set_name(std::string)'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x117): undefined reference to `employeessavitch::Employee::set_ssn(std::string)'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x155): undefined reference to `employeessavitch::HourlyEmployee::set_rate(double)'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x16b): undefined reference to `employeessavitch::HourlyEmployee::set_hours(double)'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x178): undefined reference to `employeessavitch::HourlyEmployee::get_hours() const'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x18e): undefined reference to `employeessavitch::Employee::get_name() const'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x213): undefined reference to `employeessavitch::HourlyEmployee::print_check()'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x2b0): undefined reference to `employeessavitch::SalariedEmployee::SalariedEmployee(std::string, std::string, double)'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x309): undefined reference to `employeessavitch::Employee::get_name() const'
C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o Display15.7.cpp:(.text+0x368): undefined reference to `employeessavitch::SalariedEmployee::print_check()'
c:\program files\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe C:\Users\Sikhumbu\AppData\Local\Temp\cc1bVVbs.o: bad reloc address 0xf in section `.text$_ZN16employeessavitch8EmployeeD2Ev[__ZN16employeessavitch8EmployeeD2Ev]'
C:\Dev-Cpp\SavitchChapter15\collect2.exe [Error] ld returned 1 exit status
It compiles 0 errors 0 warnings with this command:
g++ -std=gnu++17 -Wall -Wextra -pedantic-errors -pipe -O3 main.cpp salariedemployee.cpp hourlyemployee.cpp employee.cpp -o main.exe
with g++ 7.1.0 on my W7 machine.

This is the output:
Check for Might Joe for 40 hours.

________________________________________________________
Pay to the order of Might Joe
The sum of 820 Dollars
____________________________________________________________
Check Stub: NOT NEGOTIABLE
Employee Number: 12345-6789
Hourly Employee.
Hours worked: 40 Rate: 20.5 Pay: 820
______________________________________________________________

Check for Mr. Big Shot

__________________________________________________________________
Pay to the order of Mr. Big Shot
The sum of 10500.5 Dollars
_____________________________________________________________________
Check Stub NOT NEGOTIABLE
Employee Number: 987-65-4321
Salaried Employee. Regular Pay: 10500.5
_____________________________________________________________________


Thomas1965 wrote:
The .cpp need to be included in the project.

I think Thomas1965 got the point: there’s likely to be a problem in your project or in your development environment.
May be this article could be of any help:
http://www.cplusplus.com/articles/36vU7k9E/
The development of orwelldevcpp version of Bloodshed Dev-C++ seems to have been discontinued too.
Can you show us a screenshot from your project folder.

The development of orwelldevcpp version of Bloodshed Dev-C++ seems to have been discontinued too
Do you have any information about it? Last version was from November 2016
For comparison: Latest version of Code::Blocks is from January 2016
Last edited on
Thomas1965 wrote:
Do you have any information about it? Last version was from November 2016

If you mean the date which appears here
https://sourceforge.net/projects/orwelldevcpp/?source=directory
“Last Update: 2016-11-29“
that’s the date when ‘some activity’ was made on that project on sourgeforge, but if you follow the tab ‘Files’:
https://sourceforge.net/projects/orwelldevcpp/files/?source=navbar
and you let your mouse pointer hover over the link after “Looking for the latest version?”, it should appear a cloud tag which inform you that “Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.exe” was release on 2015-04-27 15:51:41 UTC. That’s not promising :-)

I don’t mean to underestimate the great work its developers have put on it. On the contrary: I want to thank them for providing us a good program for free for so many years, but unfortunately time goes by and also the ‘Review’ tab
https://sourceforge.net/projects/orwelldevcpp/reviews
is not comforting.

Thomas1965 wrote:
For comparison: Latest version of Code::Blocks is from January 2016

And it’s also slower! :-)
To be honest, I don’t think beginners should start with the minimal environment possible, which yet seems to be a widely shared opinion.
What would you recommend for beginners?
Sorry, Thomas1965, perhaps my poor English betrayed me. What I meant was perhaps beginners like me could work on some of the environments also many knowledgeable programmers like, for example Qt.
I didn’t find Qt neither harder to understand nor to install than Code::Bloks, but of course that’s absolutely personal. But Qt comes with a tool (Qt Maintenance Tool) that let you graphically install/remove/upgrade only what you really want, compiler included, which in my opinion is quite handy for a beginner.
It also have a help system, Qt Assistant, that can be set to give help also on standard C++. For example, you can download the entire cppreference.com documentation from here
http://en.cppreference.com/w/Cppreference:Archives
and feed Qt Assistant with it (I haven’t done yet, but it doesn’t seem hard to do).
I think these are ‘facilities’ a beginner could appreciate.

But now, forgive me, I’m curious :-) What about you? Is there an environment you’d recommend me (apart from the renowned Visual Studio, which I couldn’t use on my Linux machine) at least to try?
@Enoizat

Qt is perfectly fine, there may be no reason to change to something else. But which IDE one uses is a personal preference, so trying a few different ones is not a bad idea.

If you wanted to try a different IDE, there is Eclipse which I have been using for a couple of months now. The thing about it is it's use of plugins for everything. There are some 1700 plugins available. So if you wanted to do another language or script, install the plugin for it. There is even a plugin for Unix shell scripting ! There are other things too, like version control, Unit testing plus a host of other subjects. Eclipse also has a software install tool which allows updating. One can still use different frameworks like Qt, sfml etc.

The only drama I had was getting it to recognise different c++ standards. Basically one has to do settings in a couple places: One for the compiler ; another for the indexer - which is the thing that does the equivalent of Intellisense.

https://www.eclipse.org/

I also heard there is a version of VS that apparently works on Linux:

https://code.visualstudio.com/Download



@Enoizat,

I am Windows and Visual Studio guy so I can't recommend anything on Linux.
7+ years ago I played around with Ubuntu for a few weeks and KDevelop was quite nice - if I remember correctly. Not sure if it still exists. Qt creator works well on Windows.
Thank you all.
I got it right eventually

It works fine now
@Thomas1965,
thank you for all your hints.
I used Eclipse once for a PHP project and tested Visual Studio Code a few months ago, which reminded me a lot Sublime Text 3 - so much that I couldn’t see a good reason to prefer the first to the second.

I’d like a lot to continue to exchange views with you and I’m sure there’s a lot I could learn, but, luckily for you, I don’t want to exploit OP’s thread :-) so I’m not going to ask you further advice.

Bopaki wrote:
It works fine now
Good news!
Happy coding.
Topic archived. No new replies allowed.