Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Hello again fellow programmers. I'm taking a part two class of C++ for those who remember me last semester.

I've finally understood the concepts of classes fairly well, but I'm not understanding why my program is not working. Before I spill code, know that the method used is as followed:

* Header File
* Implementation File
* Test file (test methods)

XCode isn't friendly and will not let me have 'manager.h' and 'manager.cpp' in the same project. Oh well. I think my problem deals with Inheritance between two header files 'employee.h' and 'manager.h'.

Here's the implementation file:
Code:
// manager.cpp		// implements Manager class
// 01/29/2007

#include <iostream>
#include <string>

#include "manager.h"   // include declaration of Manager class
#include "employee.h"

using namespace std;

Manager::Manager(int theId, string theName)
{
    // Initialize fields inherited from parent class Employee
   
    // Initialize  fields of class Manager
    
   hrsWorked = 0;
	
   for(int i=0; i<5; i++)
      hours[i] = 0;
}

void Manager::setHours(int theHours[])
{
   for(int i=0; i<5; i++)
   {
      hours[i] = theHours[i];
      hrsWorked += hours[i];
   }
}

int Manager::getTotalHours()
{
	return hrsWorked;
}

void Manager::setRate(double payRate)
{
	ratePerHour = payRate;
}

double Manager::getRate()
{
	return ratePerHour;
}

void Manager::setBonus(double bonus)
{
	overtimePay = bonus;
}

double Manager::getBonus()
{
	return overtimePay;
}

void Manager::calcSalary()
{
	salary = hrsWorked * ratePerHour + overtimePay;
}

void Manager::printInfo()
{
	cout << "*********************************" << endl;
	cout << "Hours Worked: " << hrsWorked << endl;
	cout << "Rate per Hour: " << ratePerHour << endl;
	cout << "Overtime Pay: " << overtimePay << endl;
	cout << "Total Salary: " << salary << endl;
	cout << "*********************************" << endl;
}

Header File 'manager.h'

Code:
// manager.h		// declares Manager class
// 01/29/2007

#ifndef MANAGER_H
#define MANAGER_H

#include <string>
#include "employee.h"   // include declaration of Employee class

using namespace std;

class Manager : public Employee
{
   protected:
      int hours[5];        // number of hours worked per day
      int hrsWorked;   	   // total hours worked
      double ratePerHour;	// pay rate per hour
      double overtimePay;	// bonus
		
   public:

      // Postcondition: this name has been initialized from the given parameters
      Manager(int theId, string theName);
      
      // Postcondition: the number of hours worked per day has been set from the given array. 
      //                the total hours worked has been computed.
      void setHours(int theHours[]);
      
      // Precondition: the hoursWorked has been computed.
      // Postcondition: returns the total number of hours worked.
		
		int getTotalHours();
      
      // Postcondition: the pay rate per hour has been set from the given value. 

		void setRate(double payRate);
		
      // Postcondition: returns the payRate.
            
		double getRate();
		
      // Postcondition: the bonus pay has been set.
      
		void setBonus(double bonus);
		
      // Postcondition: returns the bonus.
      
		double getBonus();
		  
      // Postcondition: the salary has been computed.
		
		void calcSalary();
		
      // Postcondition: All information about this employee has been displayed.

		void printInfo();

      // For Extra Credits
      // Postcondition: The current manager (calling object) contains a copy of otherManager.
      // void operator= (const Manager& otherManager);
      	
      // if you would allow the assignment like a = b = c; use the following solution
      // const Manager& operator= (const Manager& otherManager);
      

}; // Manager

#endif

Finally, the test file 'testmanager.cpp'

Code:
// testmanager.cpp
// Jonathan Andrew Scott
// CSCI 2010
// 2/5/2007

#include <iostream>
#include "manager.h"
using namespace std;

int main()
{
	int hrsMgr1[5] = {5, 6, 7, 3, 6};
	int hrsMgr2[5] = {4, 5, 4, 7, 5};
	
	Manager mgr1(101, "Andrew Scott");
	Manager mgr2(102, "Kyle Gregory");
	
	cout << "Test all methods \n";
	mgr1.setHours(hrsMgr1);
	mgr1.setRate(7.00);
	mgr1.setBonus(150.00);
	mgr1.calcSalary();
	
	cout << "ID: " << mgr1.getID() << endl;
	cout << "Name: " << mgr1.getName() << endl;
	cout << "Total Hours Worked Per Week: " << mgr1.getTotalHours() << endl;
	cout << "Hourly Rate of Pay: " << mgr1.getRate() << endl;
	cout << "Bonus Pay This Week: " << mgr1.getBonus() << endl;
	cout << "Gross Pay: " << mgr1.getSalary() << endl;
	cout << endl;
	
	return 0;
}

Sorry for the indentations but you can thank the forum for that. :D Any suggestions would be appreciated.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
Would you mind:

(1) editing your post to use [code][/code] tags instead of [quote][/quote] tags (and paste the code in again) [that will preserve indents], and

(2) let us know what your problem is (won't compile?) - sorry, I'm just not clear on what you're asking

Thanks!
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Would you mind:

(1) editing your post to use [code][/code] tags instead of tags (and paste the code in again) [that will preserve indents], and

(2) let us know what your problem is (won't compile?) - sorry, I'm just not clear on what you're asking

Thanks!

Fixed on #1.

#2, I cannot get it to compile. The errors that it's giving are "manager is not defined in this scope" and my methods are returning invalid (undefined reference).
 

dcr

macrumors member
Jun 10, 2002
57
0
I think you'll find the answer here:

http://www.apsu.edu/lij/csci2010.html

under the heading

"Academic Misconduct"

You are required to turn in your own work. Collaboration on assignments and exams is prohibited, unless otherwise specified by the instructor. Likewise, plagiarism of other's work or web-related sources constitutes as a serious infraction. A penalty will be enforced for participating in academic dishonesty. Violation of academic integrity will result in a grade of 0 being assigned for the work involved or and referral to the proper university officials.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
I think you'll find the answer here:

http://www.apsu.edu/lij/csci2010.html

under the heading

"Academic Misconduct"

You are required to turn in your own work. Collaboration on assignments and exams is prohibited, unless otherwise specified by the instructor. Likewise, plagiarism of other's work or web-related sources constitutes as a serious infraction. A penalty will be enforced for participating in academic dishonesty. Violation of academic integrity will result in a grade of 0 being assigned for the work involved or and referral to the proper university officials.

We get to use his "starter files" in our assignments. Like we have to add certain methods and all that good stuff so it's legit.
 

SamMiller0

macrumors member
Aug 17, 2004
66
0
San Jose, CA
please post employee.h

Code:
samm@joule:~/code$ g++ -c -o manager.o manager.cpp
In file included from manager.cpp:7:
manager.h:8:65: error: employee.h: No such file or directory
manager.h:13: error: expected class-name before ‘{’ token
manager.cpp: In member function ‘void Manager::calcSalary()’:
manager.cpp:60: error: ‘salary’ was not declared in this scope
manager.cpp: In member function ‘void Manager::printInfo()’:
manager.cpp:69: error: ‘salary’ was not declared in this scope
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
#2, I cannot get it to compile. The errors that it's giving are "manager is not defined in this scope" and my methods are returning invalid (undefined reference).

Using the three files you posted at the top of this thread and adding the employee.h and employee.cpp from this page, it all compiles OK here with one warning (hrsMgr2 on line 13 of testmanager is unused). I'm using g++ -Wall -o testmanager testmanager.cpp manager.cpp employee.cpp to build.

I didn't bother setting up an Xcode project for this, just built and ran from the shell. For simple projects like this one, that's probably a good way to go. The output from the program looks incomplete (name is empty, for example), but I assume you're not finished yet.
 

mward333

macrumors 6502a
Jan 24, 2004
574
33
suggestion

It might be helpful, when posting questions from a university course, to let us know what the professor allows and disallows.

You pointed out that the professor provided a few files to get you started. OK!

Nonetheless, I'm still troubled by the quote: "Collaboration on assignments and exams is prohibited, unless otherwise specified by the instructor."

Can you clarify for us here? Maybe collaboration is allowed on this assignment for some reason?

(Sorry for trolling here....)
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
As far as I'm concerned, as long as people attending a class are doing their own work and only need an extra set of eyes to see what they're doing wrong, it's not a violation of anything.

I'd say that the word "collaboration" needs to be further defined if people are going to consider asking for someone to look at code collaboration. Students in the same class often do it and programmers in the real world certainly do it.

I disagree with coming to MacRumors to ask for someone else to do the assignment and someone else providing the code. It hurts that person and their future employers and usually, their co-workers.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,337
Los Angeles
Although MacRumors is not in a position to judge academic rules, we do ask that our student members and those who help them observe the spirit of such rules.

It's hard to give precise definitions of "collaboration" and "plagiarism" in this context but we can start by distinguishing between giving advice on programming in general and posting of complete ready-to-use code.

It may be perfectly fine under the assignment's rules for sixstorm to have questions answered about the meaning of a "not declared in this scope" error and what to do about it, without using code written by others. But making that interpretation is up to sixstorm and his professor.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
All I've asked around here was basically an extra set of eyes. My class has jumped into C++ and a lot of more advanced concepts that I don't completely understand yet. Heck, that's why I'm in class I guess lol.

employee.h
employee.cpp

I've already turned in the project but I still need to understand what I've been doing wrong. Trust me, I've been looking this code over for a few days but still can't get it to compile. Any suggestions or hints would really help.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
I've already turned in the project but I still need to understand what I've been doing wrong. Trust me, I've been looking this code over for a few days but still can't get it to compile. Any suggestions or hints would really help.

As before, it compiles just fine for me. The problem, it seems, must be in the way you are trying to build the project. What steps are you using to build the program? Are you required to use the Xcode system to build?

=-=-=-=

Adding: this worked fine for me in Xcode. Here's exactly how I built it.

First, I created a new project, using C++ Tool under the Command Line Utility section.

In the project window, I dragged the three .cpp and two .h files from the Finder over to the Source folder icon in Xcode, and told it to copy the files (top checkbox - everything else in the dialog was left at the defaults). Next, I removed the stock main.cpp file because it wasn't needed, told it to get rid of both files and references when it asked.

That was it. Build and Go, using the files you supplied unmodified, compiled and ran (with the one unused variable warning I mentioned earlier).
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
As before, it compiles just fine for me. The problem, it seems, must be in the way you are trying to build the project. What steps are you using to build the program? Are you required to use the Xcode system to build?

=-=-=-=

Adding: this worked fine for me in Xcode. Here's exactly how I built it.

First, I created a new project, using C++ Tool under the Command Line Utility section.

In the project window, I dragged the three .cpp and two .h files from the Finder over to the Source folder icon in Xcode, and told it to copy the files (top checkbox - everything else in the dialog was left at the defaults). Next, I removed the stock main.cpp file because it wasn't needed, told it to get rid of both files and references when it asked.

That was it. Build and Go, using the files you supplied unmodified, compiled and ran (with the one unused variable warning I mentioned earlier).

Ok I'll give it a try here soon. I don't have XCode on my laptop yet. I have been using the school's linux server to code and compile. So basically, I compile like this: "g++ employee.h manager.h manager.cpp testmanager.cpp"

I'll give your suggestion a shot this afternoon though, hopefully it might work. Last time I tried XCode with this project, I did what you did but kept getting a message asking me if I wanna replace "manager.h" with "manager.cpp". Oh well.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
So basically, I compile like this: "g++ employee.h manager.h manager.cpp testmanager.cpp"

Oh, there's your problem, you aren't including employee.cpp in your compile command. You also can leave out the *.h files, because #include in your source files will take care of that.

What you want is:

g++ -o whatever employee.cpp manager.cpp testmanager.cpp
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Oh, there's your problem, you aren't including employee.cpp in your compile command. You also can leave out the *.h files, because #include in your source files will take care of that.

What you want is:

g++ -o whatever employee.cpp manager.cpp testmanager.cpp

May I ask what the -o does?

Our professor told us to include the header files when compiling . . . so whatever. I'm not out of class yet but I'll definitly try it when I get home. Thanks!
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
May I ask what the -o does?

You use that to specify where the finished program should go, so -o whatever leaves you with an executable program named whatever.

Our professor told us to include the header files when compiling . . . so whatever.
It makes sense to add include paths (with -I) when your headers and other sources aren't in the same directory, but putting the individual includes on the command line doesn't do anything useful. The preprocessor takes care of that with #include, pulling the headers right into your .cpp files at compile time.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
iMeowbot said:
Adding: this worked fine for me in Xcode. Here's exactly how I built it.

First, I created a new project, using C++ Tool under the Command Line Utility section.

In the project window, I dragged the three .cpp and two .h files from the Finder over to the Source folder icon in Xcode, and told it to copy the files (top checkbox - everything else in the dialog was left at the defaults). Next, I removed the stock main.cpp file because it wasn't needed, told it to get rid of both files and references when it asked.

That was it. Build and Go, using the files you supplied unmodified, compiled and ran (with the one unused variable warning I mentioned earlier).

Ok, I just tried what you suggested and I'm getting the same errors:

In testmanager.cpp:
"Manager not declared in this scope"
"mgr1 not declared in this scope"
"mgr2 not declared in this scope"
Two ';' errors - Seems like it can't read from the manager header file.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Ok, I just tried what you suggested and I'm getting the same errors:

In testmanager.cpp:
"Manager not declared in this scope"
"mgr1 not declared in this scope"
"mgr2 not declared in this scope"
Two ';' errors - Seems like it can't read from the manager header file.

The compiler will also have told you the files and lines where those errors happened. Where are they?

Are you 100% certain that you are using exactly the same source code versions that you posted here (and linked here for the employee.* files)?
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
The compiler will also have told you the files and lines where those errors happened. Where are they?

Are you 100% certain that you are using exactly the same source code versions that you posted here (and linked here for the employee.* files)?

Ok, I recopied what I had listed above and I'm finally getting a successful build. I think I might have tried something this morning in the computer lab before turning it in. At least I know it compiles though . . .

Thanks for your help guys! I really appreciate it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.