某處我出錯了什麼似乎是問題?但這正是我想要做的:我想創建一個Employee類,它包含這些數據成員的名字(類型字符串),姓氏(類型字符串)和月薪(類型爲int,也是一個構造函數,用於初始化三個數據成員,爲每個數據成員提供一個集合和一個get函數,如果月薪不正,我想將其設置爲0,則創建兩個Employee對象並顯示每個對象的年薪,併爲每個Employee分配20%提高並顯示每個員工的年薪再次C++程序設置並獲得類
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee(string f ,string l ,int m){
setFName(f) ;
setLName(l) ;
setMSalary(m) ;
}
void setFName(string f){
fName = f ;
}
string getFName(){
return fName ;
}
void setLName(string l){
lName = l ;
}
string getLName(){
return lName ;
}
void setMSalary(int m){
if (m <0){
mSalary = 0 ;
}
else {
mSalary = m ;
}
}
intgetMSalary(){
returnmSalary ;
}
intySalary(){
return12 * mSalary ;
}
intraise(){
return (0.10 * ySalary()) + ySalary() ;
}
private :
string fName ;
string lName ;
int mSalary ;
};
int main(){
Employeeem1("SARA" , "SALEH" , 5000);
Employeeem2("Bayan" , "Khaled" , 8000) ;
cout<<"The yearly salary of first employee is "<< em1.ySalary() <<endl ;
cout<<"The yearly salary of second employee is "<< em2.ySalary()<<endl ;
cout<<"\nThe yearly salary of first employee is after raising "<<em1.raise()<<endl ;
cout<<"The yearly salary of second employee is after raising "<<em2.raise() <<endl ;
}
也
#include <iostream>
#include <string>
using namespace std ;
class Employee {
public:
Employee(string f ,string l ,int m){
setFName(f) ;
setLName(l) ;
setMSalary(m) ;
}
void setFName(string f){
fName = f ;
}
string getFName(){
return fName ;
}
void setLName(string l){
lName = l ;
}
string getLName(){
return lName ;
}
void setMSalary(int m){
if (m <0){
mSalary = 0 ;
}
else {
mSalary = m ;
}
}
int getMSalary(){
return mSalary ;
}
private :
string fName ;
string lName ;
intmSalary ;
};
int main(){
Employeeem1("SARA" , "SALEH" , 5000);
Employeeem2("Bayan" , "Khaled" , 8000) ;
int raise1 , raise2 ;
cout<<"The yearly salary of first employee is "<< em1.getMSalary()*12<<endl ;
cout<<"The yearly salary of second employee is "<< em2.getMSalary() *12<<endl ;
raise1 = (0.20 * em1.getMSalary()) + em1.getMSalary() ;
raise2 = (0.20 * em2.getMSalary()) + em2.getMSalary() ;
cout<<"\nThe yearly salary of first employee is after raising "<< raise1 * 12<<endl ;
cout<<"The yearly salary of second employee is after raising "<< raise*12<<endl ;
}
我得到這些錯誤
-------------------Configuration: Employee - Win32 Debug--------------------
Compiling...
Employee.cpp
C:\Users\afaq\Desktop\del\Employee.cpp(42) : warning C4183: 'intgetMSalary': member function definition looks like a ctor, but name does not match enclosing class
C:\Users\afaq\Desktop\del\Employee.cpp(46) : warning C4183: 'intySalary': member function definition looks like a ctor, but name does not match enclosing class
C:\Users\afaq\Desktop\del\Employee.cpp(50) : warning C4183: 'intraise': member function definition looks like a ctor, but name does not match enclosing class
C:\Users\afaq\Desktop\del\Employee.cpp(41) : error C2065: 'returnmSalary' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(45) : error C2065: 'return12' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(45) : warning C4552: '*' : operator has no effect; expected operator with side-effect
C:\Users\afaq\Desktop\del\Employee.cpp(49) : error C2065: 'ySalary' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(63) : error C2065: 'Employeeem1' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(64) : error C2065: 'Employeeem2' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(66) : error C2065: 'em1' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(66) : error C2228: left of '.ySalary' must have class/struct/union type
C:\Users\afaq\Desktop\del\Employee.cpp(67) : error C2065: 'em2' : undeclared identifier
C:\Users\afaq\Desktop\del\Employee.cpp(67) : error C2228: left of '.ySalary' must have class/struct/union type
C:\Users\afaq\Desktop\del\Employee.cpp(69) : error C2228: left of '.raise' must have class/struct/union type
C:\Users\afaq\Desktop\del\Employee.cpp(71) : error C2228: left of '.raise' must have class/struct/union type
C:\Users\afaq\Desktop\del\Employee.cpp(72) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
Employee.exe - 11 error(s), 5 warning(s)
確切的問題是什麼? – knightrider
我得到這些錯誤 – jhjh
我剛剛檢查了第二個代碼。您只有小空間相關的語法錯誤... – knightrider