我有不清楚的指針和字符傳遞函數的圖像。請任何人都可以告訴我在哪裏即時做錯了,簡單說明關於指針例如:我在哪裏應該使用它們,等...在C++中用指針傳遞的字符
#include <iostream>
using namespace std;
class book{
private:
char *CName;
int CFee;
int NoPeople;
int Income;
public:
void setData(char &x,int y,int z){
CName = &x;
CFee = y;
NoPeople = z;
}
void calIncome(){
Income = CFee * NoPeople;
}
void viewIncome(){
cout<<Income;
cout<<CName;
}
};
int main(){
book b1;
b1.setData('DISE',20000,30);
b1.calIncome();
b1.viewIncome();
}
即時得到錯誤在此代碼
//b1.setData( 'DISE',20000,30); 「非const左值引用類型‘字符’不能綁定到類型‘詮釋’的temparory」
πάνταῥεῖ答案是正確的,無論如何,因爲你使用的是C++而不是C,我強烈建議你使用包含sdt:string類的標準庫,它可以簡化字符的完全工作 – varantir