Sam*_*379 1 c++ constructor class
我正在学习OOP的第一步.这是我无法解决的第一个问题.此类中的max函数应返回最多两个数字.我想将数字保存在私有范围和公共范围内的函数中.但是当我想struct data{}在公共范围内使用变量时,编译器会说未声明变量.请告诉我为什么我会收到这些错误.
class myclass{
private:
struct data{
int q ;
int w;
};
public:
void get(int a, int b){
struct data = {a , b}; // here I want to pass the variables to data struct
}
int max (){ // this function returns the biggest number
if(q>w)
return q;
else
return w;
}
};
Run Code Online (Sandbox Code Playgroud)
struct data{
int q ;
int w;
};
Run Code Online (Sandbox Code Playgroud)
只声明一个类型,而不是一个对象,所以实例中没有任何q和w成员class.您需要声明一个实例的struct:
struct {
int q;
int w;
} data;
Run Code Online (Sandbox Code Playgroud)
然后,您可以写max为:
int max()
{
if (data.q > data.w)
return data.q;
else
return data.w;
}
Run Code Online (Sandbox Code Playgroud)
(我不知道你的get方法应该做什么,所以我没有替代它.)
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |