我想我正確地聲明瞭所有東西,但是我在main.cpp的第22-26行中得到了未聲明的標識符。我也得到了這個=在我的employee.cpp中12-16行不明確。我也有一個奇怪的表達式,必須有指向employee.cpp行55中工資的對象類型的指針。我非常感謝任何幫助,因爲我的學校目前沒有導師。將成員變量設置爲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 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
後實際錯誤。 – chris
你有沒有'#include'? –
Joseph
並且向你的代碼添加提示,哪一行是第22行,即標記發生錯誤的行。 –