2013-10-12 3 views
0

내 헤더 파일에 friend 함수를 선언하고이를 내 .cpp 파일에 정의했지만 컴파일 할 때 '이 범위에서 변수가 선언되지 않았습니다'라는 메시지가 나타납니다. 함수가 클래스의 친구로 레이블이 지정되면 해당 클래스의 모든 멤버에 직접 액세스 할 수 있으므로이 오류가 발생하는 이유는 무엇입니까?내 친구 함수가 실행되지 않습니다.

내 .H 파일 : my.cpp 파일

#ifndef EMPLOYEE_H 
#define EMPLOYEE_H 

#include<string> 
using namespace std; 

class Employee 
{ 
    friend void SetSalary(Employee& emp2); 

private: 

    string name; 
    const long officeNo; 
    const long empID; 
    int deptNo; 
    char empPosition; 
    int yearOfExp; 
    float salary; 
    static int totalEmps; 
    static int nextEmpID; 
    static int nextOfficeNo; 

public: 
    Employee(); 
    ~Employee(); 
    Employee(string theName, int theDeptNo, char theEmpPosition, int theYearofExp); 
    void Print() const; 
    void GetInfo(); 
}; 

#endif 

기능

void SetSalary(Employee& emp2) 
{ 

    while (empPosition == 'E') 
    { 
     if (yearOfExp < 2) 
     salary = 50000; 
     else 
     salary = 55000; 
    } 
} 

참고 : 나의하여 Main.cpp에서, 나는 'emp2를'객체를 생성하고있다. 이 함수에 매개 변수로 전달됩니다.

답변

4

empPosition, yearOfExpsalaryEmployee 클래스의 회원 yearOfExpsalary 관련된 표현에 대한

while (emp2.empPosition == 'E') .... 
//  ^^^^ 

유사 필요합니다. friend 함수는 비 멤버 함수이므로 해당 클래스의 인스턴스 (이 경우 emp2)를 통해 자신이 속한 클래스의 데이터 멤버에만 액세스 할 수 있습니다.

+0

와우 ... 헤더와 .cpp 사이를왔다 갔다하면서 나는 그것에 대해 한 번도 말하지 않았다. 어젯밤에 그걸 알아야 했어. – Kevin

관련 문제