如何在循环中从数组对象访问成员函数
How access member function from arrayobject in a loop
我正在尝试访问公共函数get_data(),以生成输出"这里",以查看是否从对象数组创建动态对象。那我该怎么做。
#include<iostream>
#include <conio.h>
using namespace std;
int counts = 0;
int no_of_array;
class Matrix
{
int **dynamicArray;
public:
Matrix()
{
counts++;
dynamicArray = new int *[3];
for (int i = 0; i < 3; i++)
{
dynamicArray[i] = new int[3];
}
}
void get_data(){
cout << "here n";
}
};
int main()
{
int newvalue = 1;
int i;
Matrix *array_object[100];
int choice;
while (newvalue == 1){
cout << "Enter your choice n 1. for 2 matrix addition n 2. for 2 matrix multiplication n";
cin >> choice;
if (choice == 1)
{
cout << "how many array to add n";
cin >> no_of_array;
for (i = 0; i <= no_of_array; i++)
{
array_object[i] = new Matrix();
}
for (i = 0; i < no_of_array; i++)
{
array_object[i].get_data();
}
}
else if (choice == 2)
{
Matrix mul;
// mul.multiplication();
}
else
cout << "Do enter correct choice n";
cout << "press 1 to enter again and 0 to exit n";
cin >> newvalue;
}
getch();
return 0;
}
我正在尝试在这里检查,对于创建的所有对象,get_data函数将被调用或不被调用......但相反,我得到一个错误get_data
尚未清除。
使用 array_object[i]->get_data();
而不是 array_object[i].get_data();
。
当对象尝试访问其类成员函数/变量时,将使用DOT(.)
运算符Arrow(->)
而如果对象是指针,则使用运算符。
现在,您宣布
Matrix *array_object[100];
这意味着array_object
是一个Matrix
指针数组。因此,您需要使用Arrow(->)
而不是DOT(.)
运算符。
Matrix *array_object[100];
是指向矩阵类型的数组的指针。为了使用指针访问类成员,您应该使用->
运算符。
for(i=0;i<no_of_array;i++)
{
array_object[i]->get_data();
}
相关文章:
- 按基类对象访问派生类资源时出错
- 使用基类对象访问派生的仅类方法
- 从 const 对象访问非 const 方法
- 如何在C++中使用类对象访问指针数据成员
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 为什么引用类型在使用临时对象访问时是左值
- 从父对象 c++ 访问子方法
- 无法通过指针或对象 c++ 访问受保护的成员
- 为什么可以从类中的对象访问类的私有变量
- 对类似"struct {double, int}"对象使用reinterpret_cast进行对象访问
- 通过无符号 char 别名进行对象访问,加载和存储时会发生什么?
- 从对象访问结构枚举
- 如何全局创建对象并使用该对象访问全局范围内的公共成员函数
- 在C 中,可以从第三个共享对象访问其他两个不同共享对象
- 如何在C 中提供一个可呼叫的对象访问,例如lambda
- C 向量对象访问
- C++是否可以从同级第二个派生对象访问第一个派生对象的受保护基础数据成员?
- 如何从类内实例化的对象访问私有变量
- 多态性:通过类文本或对象访问静态成员