[英]How can I call name places from an array in a class object?
How would I connect an array in a header file to a class to print a name from the array?我如何将 header 文件中的数组连接到 class 以打印数组中的名称?
I have something like the following.我有类似以下内容。 I want to use the array from the header file to pick which name to print, so 1 would be Jill.
我想使用 header 文件中的数组来选择要打印的名称,因此 1 就是 Jill。 Instead of typing
Obj.setFirst_name("Jill")
I want to type Obj.setFirst_name(1)
, how would I do that?而不是键入
Obj.setFirst_name("Jill")
我想键入Obj.setFirst_name(1)
,我该怎么做?
source来源
#include <iostream>
using namespace std;
class Thing{
private:
string first_name;
public:
setFirst_name(string First){
First_name = First;
}
string getFirst_name(){
return First_name;
}
};
int main() {
Thing Obj;
Obj.setFirst_name(1);
cout << Obj.getFirst_name(1) << endl;
}
header header
string First [2] = {"Jack","Jill"};
I have tried different variations of the getters and setters.我尝试了 getter 和 setter 的不同变体。
Just do做就是了
Thing Obj;
Obj.setFirst_name(First[1]);
cout << Obj.getFirst_name() << endl;
BTW: their is no relationship between the function arg called First and the global array called First.顺便说一句:它们在名为 First 的 function arg 和名为 First 的全局数组之间没有关系。 Apart from the fact that the argument hides the global inside that set function
除了这个论点隐藏了集合 function 的全局这一事实
EDIt编辑
or do this或者这样做
class Thing{
private:
string first_name;
public:
setFirst_name(int idx){
First_name = First[idx];
}
string getFirst_name(){
return First_name;
}
}
;
now this works现在这有效
Obj.setFirst_name(1) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.