如何从另一个类访问数组
How to access an array from another class?
>我试图从密码游戏类访问我的数组y,我在密码游戏类中公开声明了数组,以便在我的其他类中可以访问它。
这是我过去在 y 什么是全局变量时所拥有的。
for (int j =0; j < 4; j ++ ) {
bool flag = false;
if ( x[j] == y[j] ) {
cout << "O";
finish[j] = true;
continue;
}
现在作为类 PasswordGame 中的 y 变量我这样做,我的错误是"成员引用基类型"int"不是结构或联合"。
PasswordGuessingGame u;
for (int j =0; j < 4; j ++ ) {
bool flag = false;
if ( x[j] == u.y[j] ) {
cout << "O";
finish[j] = true;
continue;
}
但是,如果我公开声明,它会起作用,但在循环中无法正常工作。
public:
int two = u.y[j];
谢谢!
我不知道
你是如何声明你的类定义的。但这里只是为了演示一个简单的例子:
#include <iostream>
class PasswordGuessingGame{
public :
int x[5]={1,2,3,4,5};
};
class anotherClass{
public :
int y[5]={11,22,33,4,6};
};
int main(){
PasswordGuessingGame u;
anotherClass v;
int finish[5];
for (int j =0; j < 4; j ++ ) {
bool flag = false;
if ( u.x[j] == v.y[j] ) {
std::cout << "O";
finish[j] = true;
}
}
return 0;
}
相关文章:
- 在 C++ 中访问数组负索引处的内存不会返回垃圾
- 使用指针访问数组中的对象数据成员
- VTK 图像数据,访问数组中的 1 个图像
- 调用函数后无法访问数组
- 如何从地图访问数组的元素?
- 访问数组中的不同元素
- 使用指针访问数组元素时出现意外结果
- 使用指针访问数组的值
- C++基于1而不是0的访问数组,方法是在编译时移位
- 如何在主函数之外访问数组?
- 如何在 llvm 的 cpp api 中使用 CreateInBoundsGEP 来访问数组的元素?
- 访问数组中所有结构的成员
- C 是否每次访问数组时都复制元素?
- C++ 为什么访问数组中的元素会返回指针?
- 如何通过指针访问数组的元素
- 指针数组交换无需直接访问数组
- 直接从指针/地址访问数组的元素
- 我有一个 Qt 对象的 2d 数组,我已经用值播种了这些对象。如何访问数组中特定对象的值并更改它们?
- 如何使用 C+ 中的指针访问数组中的特定索引
- 通过两个下标访问数组成员