所以我需要得到結構#1(e [0])的代碼,但我得到以下錯誤; 「錯誤:'emp1'中的成員'get_code'的請求,它是指針類型'Employee *'(也許你打算使用' - >'?)」 我真的不明白如何解決這個問題。此外,這是一個分配,所以我必然會使用結構,而且,我不知道「 - >」是什麼,但如果它是任何操作員或其他東西,我不允許使用它,因爲我們沒有教過那呢。 (答案爲類似問題建議使用 - >所以對我來說,這並不工作) 我也嘗試使用*(EMP1).get_code()指向一個結構數組中的結構
#include <iostream>
#include <string.h>
using namespace std;
struct Employee{
private:
string code;
string name;
float salary;
public:
void set_code(string c){
code=c;
}
void set_name(string n){
name=n;
}
void set_sal(float s){
salary=s;
}
string get_code(){
return code;
}
string get_name(){
return name;
}
float get_sal(){
return salary;
}
};
int main(void) {
Employee e[2],*emp1,*emp2;
string c,n;
float s;
for (int i=0;i<2;i++){
cout<<"Enter code for employee "<<i+1;
cin>>c;
e[i].set_code(c);
cout<<"Enter name for employee "<<i+1;
cin>>n;
e[i].set_name(n);
cout<<"Enter salary for employee "<<i+1;
cin>>s;
e[i].set_sal(s);
}
*emp1=e[0];
cout<<emp1.get_code();
}
關於該錯誤消息的含義尚不清楚? –
錯誤是告訴你使用'emp1-> get_code()'。 'x-> y()'通常相當於'(* x).y()'。 –
_「我也嘗試過使用'*(emp1).get_code()'」_非常接近! –