将成员变量设置为null不正确

时间:2013-10-08 21:22:17

标签: c++

我想我正在宣布一切正确但我在main.cpp的第22-26行中获得了未声明的标识符。我也得到这个=在我的employee.cpp第12-16行中含糊不清。我还有一个奇怪的表达式必须有一个指向employee.cpp第55行的工资对象类型的指针。我真的很感激任何帮助,因为我的学校目前没有导师。

//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 FirstName, string LastName, float Salary);
    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, string FirstName, string LastName, float Salary)
{
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 >> LastName[count];
        MyinFile >> FirstName[count];
        MyinFile >> **Salary[count];** //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, FirstName, LastName, Salary**); //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成员函数。

以下是错误:

------ Build started: Project: Lab3Project, Configuration: Debug Win32 ------
Main.cpp
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(22): error C2065: 'FirstName' : undeclared identifier
 \\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(22): error C2065: 'LastName' : undeclared identifier
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(22): error C2065: 'Salary' : undeclared identifier
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(25): error C2065: 'SearchName' : undeclared identifier
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(25): error C3861: 'LastName': identifier not found
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(26): error C2065: 'DisplayFirstName' : undeclared identifier
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(26): error C2065: 'DisplayLastName' : undeclared identifier
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\main.cpp(26): error C2065: 'DisplaySalary' : undeclared identifier

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(40): error C2109: subscript requires array or pointer type
\\psf\home\documents\visual studio 2010\projects\csci112\lab3project\lab3project\employee.cpp(55): error C2109: subscript requires array or pointer type

2 个答案:

答案 0 :(得分:2)

  • 看起来您需要做很多工作来改进代码。首先,您应该将所有头文件放在Employee.h上。这在您决定包含Employee.h的任何地方都很有用,它的所有头文件也将被包含在内,因此您不必再次包含它们。
  • 在C ++中,您可以将变量定义为指针或引用。请学习指针(*指针),双指针 ** doublepointer)之间的区别,也称为数组指针,引用(&amp; reference)和变量(变量)。您还应该学习如何对指针(**指针)进行取消引用。
  • 了解类,函数或变量定义声明之间的差异。这对您当前的分配问题尤为重要。
  • 你有一个员工班,如果你是初学者;头文件用于构造函数,析构函数,成员变量和成员函数定义。而.cpp文件用于构造函数,成员函数和非成员函数声明。大多数情况下,析构函数的声明不包含任何内容,但是这是删除指针的地方。
  • 在C ++中,你必须管理内存,否则你的指针会出现内存泄漏。因此,无论何时定义指针(*指针),都应该在析构函数中删除它。您在头文件中缺少析构函数。你需要定义一个。
  • 您应该像这样在头文件中定义和/或声明构造函数。实际上没有必要在构造函数中调用SetFirstName(),setLastName(),setSalary()。这些方法只能在MAIN类中使用。要设置类成员变量,请执行此操作。

    Employee( string FirstName, string LastName, float Salary )
    this.FirstName = FirstName;
    this.LastName = LastName;
    this.Salary = Salary;
    

    }

  • 包含类析构函数

    ~Employee(){
    }
    
  • 您的SET函数仅用于设置成员变量,不应返回任何内容。而不是返回一个字符串,他们应该是无效的。像这样。

  • 设置功能定义。

    void setFirstName(string FirstName);
    void setLastName(string LastName);
    void setSalary(float Salary);

  • 定义并声明GET函数。

    string getFirstName(){return FirstName;}
    string getLastName(){return LastName;}
    float getSalary(){return Salary;}

  • 在.cpp文件中,您应该在Employee类中定义的其他成员函数中声明您的set函数。像这样。

    void Employee :: setFirstName(string FirstName){
    FirstName = FirstName;
    }
    void Employee :: setLastName(string LastName){
    LastName = LastName;
    }
    void Employee :: setSalary(float Salary){
    薪水=薪水;
    }

  • 此Readfile函数读取此格式示例的文件;第一行;约翰加里100.50,下一行; Michael Shawn 250.80等并将其存储在Employees的矢量中。

    typedef std :: vector&lt;员工&gt; EmployeeType
    EmployeeType帐户;​​

    void ReadFile(ifstream&amp; MyinFile,string FirstName,string LastName,float Salary){
    string st;
    float n;

    while(MyinFile&gt;&gt; st){
    this.setFirstName(ST);
    this.setLastName(MyinFile&gt;&gt; st);
    this.setSalary(MyinFile&gt;&gt; n);
    account.push_back(本);
    }

  • 搜索员工功能。它仅按名字搜索。

  • 您还应该了解C ++中的数组和数组指针。

    string Employee :: EmployeeSearch(string LastName,string FirstName){

    cout&lt;&lt; “请输入您要搜索的员工的姓名。” &LT;&LT; ENDL;
    cin&gt;&gt; SearchName;
    string st =“没有这样的员工”;
    string s =“”;

    员工*雇用;

    for(int i = 0; i&lt; account.size(); i ++){
    雇用= account.at(i);
    if(SearchName == employ-&gt; getFirstName()){
    std :: cout&lt;&lt;雇用 - > getFirstName()&lt;&lt; “”&lt;&lt;雇用 - > getLastName()&lt;&lt; “”&lt;&lt;雇用 - > getSalary()&lt;&lt; ENDL;
    st =“”;

    st.append(employ.getFirstName());
    st.append(一个或多个);
    st.append(employ.getLastName());
    st.append(一个或多个);

    std :: ostringstream ss;
    ss&lt;&lt; employ.getSalary();
    string so(ss.str());
    st.append(左右);
    返回st;
    }
    } 返回st;

    }

答案 1 :(得分:1)

请看main.cpp中的这一行:

Employee.ReadFile(**MyinFile, FirstName, LastName, Salary**); //undeclared identifier error

FirstNameLastNameSalary来自哪里?他们从未在int main()的范围内宣布。这就是您有未声明的标识符错误的原因。

我不确定为什么你的Employee::ReadFile成员函数会接受最后三个参数。它应该从文件中读入并设置this.FirstName等,而不是已传递给函数的参数。

这可以让你在正确的轨道上走得更远:

// In header file, replace ReadFile prototype with:
void ReadFile(ifstream& MyinFile);

// In implementation file, replace ReadFile with:

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;
        }
    MyinFile.close();
    }

// In main.cpp, replace the current call to Employee.ReadFile with:
Employee.ReadFile(MyinFile);

这可能无法解决您的所有问题,但这是一个开始。