Hi fellas,

This week in class (2nd week), we are learning about classes. Our professor has asked us just to complete one simple class with separate files. My problem is when I go to compile, I get errors stating my variables aren't "declared in this scope." As far as I understand declarations, I thought I declared them in my salary.cpp file.

Can you help me out in uncovering what the compiler is needing? Everything looks declared to me.

SALARY.H

#ifndef salary_h
#define salary_h
#include <iostream>
#include <string>

using namespace std;


class EmployeeSalary
{
private:
	string employeeName;
	float hrlyWage;

public:
	EmployeeSalary(string name, float hrlyWage);

	string getName() const;
	float gethrlyWage() const;

	float paycheck(float hrsWorked) const;
};

#endif

SALARY.CPP

#include "salary.h"
#include <iostream>
#include <string>



EmployeeSalary::EmployeeSalary(string name, float hrlyWage)
{
	wholeName = name;
	wage = hrlywage;
}

string EmployeeSalary::getName() const
{
	return wholeName;
}

float EmployeeSalary::getPayRate() const
{
    return wage;
}

float EmployeeSalary::paycheck(float hrsWorked) const
{
	return hrsWorked * hrlyWage;
}

MAIN.CPP

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

using namespace std;

void purpose()
{
	cout <<" " <<endl;
	cout <<"==============================================================================" <<endl;
	cout <<"\t\t\tPersonal Notes:" <<endl;
	cout<<"\n\n\nThis *class* exercise is to illustrate the mechanics of using a class in OOP." <<endl;
	cout <<"Header File (salary.h): contains the class definition, class properties & methods:" <<endl;
	cout <<"\n\nPublic - makes class variables & methods accessible to any part of the program." <<endl;
	cout <<"Private - variables and functions can only be accessed from within the class itself." <<endl;
	cout <<"\n\n Source File (salary.cpp): this is where write your method implementations and return values." <<endl;
	cout <<"Main File (main.cpp): contains all classes and methods used to initiate program." <<endl;
	cout <<"\n==============================================================================" <<endl;
}

int main()
{
	float hrsWorked;
	string wholeName;
	float wage;

	cout <<"Please enter your name: "
	cin >> wholeName
	EmployeeSalary::getName();

	cout <<"\nPlease enter your hourly rate: "
    cin >> wage
    EmployeeSalary::getPayRate(wage);

	cout <<"Please enter how many hours you worked this week: "
    cin >> hrsWorked;

	cout <<"You will be paid " <<EmployeeSalary::paycheck(float hrsWorked) "this week."

cin.get()
return 0;
}

Forgive me if the code looks illogical...I am trying to wrap my head around class mechanics.

Thanks!

Forgot to mention what variables the compiler is complaining about not being declared:

Line 10: wage
Line 10: hrlyWage
Line 9: wholeName

and an odd error (to me) that says:
Line 18: no 'float EmployeeSalary::getpayRate() const' member function declared in class 'EmployeeSalary'

ALL of these errors are pointing to the SALARY.CPP file.

You have wage declared in main, and hrlywage declared as a member of EmployeeSalary . You then try to use wage in member functions defined for EmployeeSalary . But wage isn't visible inside these member functions, because it's not global to the project.

Also, I think you're just getting your variables mixed up. In the EmployeeSalary constructor, you're trying to set wage to the provided wage, but wage is just a variable in main, not a member variable.

Basically, wage is invisible from inside EmployeeSalary , and all the variables declared inside EmployeeSalary are invisible in main. This is just how C++ scoping rules work.

Looking at your code it appears that different people wrote the .h , .cpp and main file. The functions declared in the .h file are different from the ones defined in the .cpp file, which again are different from the ones called in the main file. Please look carefully, the function name, parameters, return types should match.

Also you cannot call non-static member functions using scope resolution operator '::'. You will have to create an object of the class and call the function.

Looking at your code it appears that different people wrote the .h , .cpp and main file. The functions declared in the .h file are different from the ones defined in the .cpp file, which again are different from the ones called in the main file. Please look carefully, the function name, parameters, return types should match.

Also you cannot call non-static member functions using scope resolution operator '::'. You will have to create an object of the class and call the function.

Thanks all for trying to talk me through this. I tried to take your advice and transform it into what I thought you all meant. However, I'm getting a new error.

It says, "Line 8: error returning a value from constructor" (salary.cpp)

I'm having a heck of time trying to make this logical in order to make it work correctly. Am I close in solving the constructor dilemma? Or perhaps, I'm just missing one thing in making it correct?

Here is the problem child source file: (if the *.h and main() file is needed, I can post that too. I just didn't want to post unneeded code).

SALARY.CPP

#include "salary.h"
#include <iostream>
#include <string>


EmployeeSalary::EmployeeSalary(string name)    //I do not fully understand how to make constructors logical
{
   return name;
}

string EmployeeSalary::getName()
{
   	return name;
}

float EmployeeSalary::gethrlyWage()
{
    return hrlyWage;
}

float EmployeeSalary::pay(float hrsWorked)
{
	return hrsWorked * hrlyWage;
}

The error clearly says that you cannot return a value from a constructor. Look the signature of a constructor, do you see any return type? Constructors cannot have a return statement.
You should use a constructor if you want to initialize class members or if not that do not declare one and the compiler will generate a default constructor for your class. I would suggest you read about constructors a bit more.

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.