0

我在使用 this-> 时遇到问题,因为它只能在非静态成员函数中引用。我也遇到了变量 = null 的问题,因为它说“=”是模棱两可的。

//Employee.h

using namespace std;

class Employee {
private:
public:
    string FirstName;
    string LastName;
    string DisplayFirstName;
    string DisplayLastName;
    string DisplaySalary;
    string SearchName;
    float Salary;
    Employee( string FirstName, string LastName, float Salary )
    {
            setFirstName(FirstName);
            setLastName(LastName);
            setSalary(Salary);
    }
    string setFirstName(string FirstName);
    string setLastName(string LastName);
    float setSalary(float Salary);
    void ReadFile(ifstream& MyinFile);
    string EmployeeSearch(string LastName[], string SearchName);
    void DisplayEmployee (string DisplayFirstName, string DisplayLastName, string DisplaySalary);
    Employee();
};

//Employee.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Employee.h"

using namespace std;

string setFirstName(string FirstName)
{
**FirstName = NULL;** //ambiguous error
}
string setLastName(string LastName)
{
**LastName = NULL;** //ambiguous error
}
float setSalary(float Salary)
{
Salary = 0.0;
}
void ReadFile(ifstream& MyinFile)
{
char exit_char;
int MaxSize;
int count = 0;

MyinFile.open("employee.dat"); 
    if (!MyinFile)
    {    //no
        cout << "Can't open input file." << endl; //Tests the right file.
        char exit_char;                         //End Program
        cout << "Press any key to exit" << endl;
        cin >> exit_char;
    }
    for(count = 0; count < MaxSize; count++)
    {
        MyinFile >> **this->LastName**;
        MyinFile >> **this->FirstName**;
        MyinFile >> **this->Salary**; //error
    }
MyinFile.close();
}
string EmployeeSearch(string LastName[], string FirstName[], float Salary, string SearchName, string DisplayFirstName, string DisplayLastName, string DisplaySalary)
{
    cout << "Please enter the name of the employee you would like to search." <<  endl;
    cin >> SearchName;

    for (int i = 0; i < 10; i++ )
    {
        if (LastName[i] == SearchName)
    {
        DisplayFirstName = FirstName[i];
        DisplayLastName = LastName[i];
        DisplaySalary = **Salary[i];**  //error
    }
    else 
        cout << "Could not find employee." << endl;
    }
};
void DisplayEmployee (string DisplayFirstName, string DisplayLastName, string DisplaySalary)
{
cout << DisplayFirstName << "   ";
cout << DisplayLastName << "    ";
cout << DisplaySalary << endl;
};

//Main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Employee.h"

using namespace std;

const int MaxSize = 100;

int main()
{
char Redo;          //Input a character to redo the program
ifstream MyinFile;
cout << "Your Salary Machine\n\n";
Employee  Employee;
Employee.ReadFile(MyinFile); //undeclared identifier error
do
{
    Employee.EmployeeSearch(**LastName[], SearchName**); //undeclared identifier error
    Employee.DisplayEmployee(**DisplayFirstName,DisplayLastName,DisplaySalary**); //undeclared identifier error
    //Asks user if they want redo the program
    cout << "Would you like to redo the program?\n";
    cout << "Please enter Y or N: \n";
    cin >> Redo;
}while(Redo == 'Y' || Redo == 'y');

return 0;
}

编写该程序以读取具有名字和姓氏的文件,然后是薪水,然后能够输入文件中任何人的姓氏,它将显示姓名和薪水,然后重复。我假设使用构造函数将名字和姓氏初始化为 NULL,然后将薪水初始化为 0.0。我还应该使用 get 和 set 成员函数。

以下是错误:

 Main.cpp
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(31): error C2059: syntax error : ']'
 Employee.cpp
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(12): error C2593: 'operator =' is ambiguous
      c:\program files\microsoft visual studio 10.0\vc\include\xstring(772): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
      with
      [
          _Elem=char,
          _Traits=std::char_traits<char>,
          _Ax=std::allocator<char>
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\xstring(767): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
      with
      [
          _Elem=char,
          _Traits=std::char_traits<char>,
          _Ax=std::allocator<char>
      ]
      while trying to match the argument list '(std::string, int)'
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(16): error C2593: 'operator =' is ambiguous
      c:\program files\microsoft visual studio 10.0\vc\include\xstring(772): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
      with
      [
          _Elem=char,
          _Traits=std::char_traits<char>,
          _Ax=std::allocator<char>
      ]
      c:\program files\microsoft visual studio 10.0\vc\include\xstring(767): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
      with
      [
          _Elem=char,
          _Traits=std::char_traits<char>,
          _Ax=std::allocator<char>
      ]
      while trying to match the argument list '(std::string, int)'
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(38): error C2355: 'this' : can only be referenced inside non-static member functions
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(38): error C2227: left of '->LastName' must point to class/struct/union/generic type
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(39): error C2355: 'this' : can only be referenced inside non-static member functions
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(39): error C2227: left of '->FirstName' must point to class/struct/union/generic type
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(40): error C2355: 'this' : can only be referenced inside non-static member functions
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(40): error C2227: left of '->Salary' must point to class/struct/union/generic type
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(55): error C2109: subscript requires array or pointer type
4

3 回答 3

3

MemberFirstNamestd::string(object) 的一个实例,因此试图分配NULL给它是没有意义的。

另请注意,要在其他地方(不是声明它的地方)定义成员函数,您必须使用
类的名称作为前缀:

string Employee::setFirstName(string FirstName)
{
    ...
}

还要注意,您的构造函数可能会简化为此(使用初始化列表):

Employee (string FirstName, string LastName, float Salary)
  : FirstName(FirstName), LastName(LastName), Salary(Salary) { }
于 2013-10-08T22:26:07.917 回答
2

类声明:

class Employee {
public:
    string FirstName;
    string LastName;
...
    string EmployeeSearch(string LastName[], string SearchName);
    void DisplayEmployee (string DisplayFirstName, string DisplayLastName, string DisplaySalary);
    Employee();
};

类实现:

string 
Employee::EmployeeSearch(string LastName[], string FirstName[], float Salary, string SearchName, string DisplayFirstName, string DisplayLastName, string DisplaySalary)
{
    cout << "Please enter the name of the employee you would like to search." <<  endl;
    cin >> SearchName;
    ...

如果没有“Employee::”语法,“EmployeeSearch() 只是一个独立的函数,与您的 Employee 类无关。

还:

**FirstName = NULL并且**this->LastName**是错误的。

如果 "Firstname" 是一个字符串成员,您可以直接按名称引用它。同样,如果 ReadFile() 是成员函数 (Employee::ReadFile),则不需要“this->”。

于 2013-10-08T22:26:47.247 回答
0

  • 将所有数据成员保密,这将提供封装,这就是 OOP 的意义所在。
  • 为每个私有成员构建一个 Get 和 Set 函数,以便您可以访问它
  • 当你声明一个字符串时,它的默认值会是一个空字符串,所以你不需要设置它等于null
  • 对于 set 函数,它不返回任何值,所以它需要是 void 函数。
  • 对于 get 函数,由于它只返回而不改变值,所以你应该在它后面加上关键字 const。
  • 对于构造,可以设置参数等于私有成员,不需要调用set函数。
  • Read、Search 和 Display 函数应该放在主类中,而不是 Employee 类中。它会更容易维护。

员工.h

class Employee
{
    private:
    std::string FirstName;
    std::string LastName;
    float Salary;
    public:
    Employee() {}
    Employee(std::string newFirstName, std::string newLastName, float newSalary);
    std::string GetFirstName() const {return FirstName;}
    std::string GetLastName() const {return LastName;}
    float GetSalary() const {return Salary;}
    void SetFirstName(string val) {FirstName = val;}
    void SetLastName (string val) {LastName = val;}
    void SetSalary(float val) {Salary = val;}
};

员工.cpp

Employee::Employee(std::string newFirstName, std::string newLastName, float newSalary)
{
    FirstName = newFirstName;
    LastName = newLastName;
    Salary = newSalary;
}
于 2013-10-08T23:26:54.910 回答