Brett Holmes
Brett Holmes

Reputation: 387

I have a error with using this-> in c++

I'm having problems with using this-> as it is saying it can only be referenced in a nonstatic member function. I also am having a problem with the variable = null as it says "=" is ambiguous.

//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;
}

The program is written to read a file that has a first and last name then salary then be able to type in the last name of whoever is in the file and it will display the name and salary and then repeat. I'm suppose to use a constructor to initialize the first and last name as NULL and then the salary as 0.0. I'm also supposed to use get and set member functions.

Here are the errors:

 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

Upvotes: 0

Views: 922

Answers (3)

Hoang Minh
Hoang Minh

Reputation: 1220

  • Put all the data members in private, that will provide encapsulation and that's what OOP is about.
  • Build a Get and Set function for each private member, so you can access to it
  • When you declare a string, its default value will be an empty string, so you don't need to set it equal to null
  • For the set function, it does not return any value back, so it needs to be void function.
  • For the get function, since it only returns and does not change the value, you should put the keyword const behind it.
  • For the construction, you can set the parameters equal to private members, and don't need to call the set functions.
  • Read, Search and Display functions should be put in the main, and not the Employee class. It will be easier to maintain.

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;}
};

Employee.cpp

Employee::Employee(std::string newFirstName, std::string newLastName, float newSalary)
{
    FirstName = newFirstName;
    LastName = newLastName;
    Salary = newSalary;
}

Upvotes: 0

paulsm4
paulsm4

Reputation: 121669

Class declaration:

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

Class implementation:

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;
    ...

Without the "Employee::" syntax, "EmployeeSearch() is just a standalone function, having nothing to do with your Employee class.

ALSO:

**FirstName = NULL and **this->LastName** are wrong.

If "Firstname" is a string member, you can just refer to it by name. Similarly, if ReadFile() is a member function (Employee::ReadFile), then you don't need "this->".

Upvotes: 2

LihO
LihO

Reputation: 42093

Member FirstName is an instance of std::string (object) thus trying to assign NULL to it makes no sense.

Also note that to define a member function in other place (not where it was declared), you must use
a class's name as a prefix:

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

And also note that your constructor might be simplified to this (using initialization list):

Employee (string FirstName, string LastName, float Salary)
  : FirstName(FirstName), LastName(LastName), Salary(Salary) { }

Upvotes: 3

Related Questions