C++ 如何从循环中的arrayobject访问成员函数
我试图访问公共函数get_data(),以生成一个输出“here”,查看是否从对象数组创建动态对象。那个么我该怎么做呢C++ 如何从循环中的arrayobject访问成员函数,c++,oop,C++,Oop,我试图访问公共函数get_data(),以生成一个输出“here”,查看是否从对象数组创建动态对象。那个么我该怎么做呢 #include<iostream> #include <conio.h> using namespace std; int counts = 0; int no_of_array; class Matrix { int **dynamicArray; public: Matrix() { counts++
#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;
}
#包括
#包括
使用名称空间std;
整数计数=0;
数组的int no_;
类矩阵
{
int**动态卡雷;
公众:
矩阵()
{
计数++;
dynamicArray=新整数*[3];
对于(int i=0;i<3;i++)
{
dynamicArray[i]=新整数[3];
}
}
void get_data(){
不能选择;
如果(选项==1)
{
cout>no\u的_数组;
对于(i=0;i使用array_object[i]->get_data();
而不是array_object[i]。get_data();
当对象尝试访问其类成员函数/变量时,使用点(.)
运算符,而如果对象是指针,则使用箭头(->)
运算符
现在,你宣布
Matrix *array_object[100];
这意味着array\u对象
是一个矩阵
指针数组。因此,您需要使用箭头(->)
而不是点(.)
运算符。矩阵*数组对象[100];
是指向矩阵类型数组的指针。为了使用指针访问类成员,您应该使用->
运算符
for(i=0;i<no_of_array;i++)
{
array_object[i]->get_data();
}
for(i=0;iget_数据();
}