編譯器說,A和B是不是在範圍中聲明:這個程序有什麼問題?說A和B不在範圍
#include <iostream>
using namespace std;
class sample {
private:
int a, b;
public:
void setvalue() {
a=25; b=40;`enter code here`
}
friend int sum(sample s1); //says a and b are not in the scope
};
int sum(sample s1) {
return a+b; //says a and b are not in the scope
}
int main() {
sample x;
x.setvalue();
cout<<"\nSum ="<<sum(x);
return 0;
}
尋找過去可怕的編輯(請修復它)它看起來像你試圖訪問全局函數中的'a'和'b'('sum')。 *你想用誰的'a'和'b'?你需要一個'sample'的實例。你的意思是使用's1.a'等嗎? – Biffen
預計多次降價,直到您修復代碼,這是目前廢話。 –