c++二维指针和二级指针的问题
#include<iostream>#include<cstring>usingnamespacestd;classMatrix{public:int**p;intr;i...
#include <iostream>
#include <cstring>
using namespace std;
class Matrix {
public:
int ** p;
int r ;
int c ;
Matrix() {
int r = 0;
int c = 0;
int **p = NULL;
}
friend istream &operator>>(istream &o, Matrix &a) {
o >> a.r >> a.c;
int **p = new int*[a.r];
for (int i = 0; i < a.r; i++) {
p[i] = new int[a.c];
}
for (int i = 0; i < a.r; i++) {
for (int j = 0; j < a.c; j++) {
o >> a.p[i][j];
}
}
return o;
}
};
int main() {
Matrix a, b;
cin >> a >> b;
return 0;
}
求问各位大佬,我想通过运算符重载录入一个二维数组,但是编译时引发了异常: 读取访问权限冲突。
a.p 是 0x1110112 展开
#include <cstring>
using namespace std;
class Matrix {
public:
int ** p;
int r ;
int c ;
Matrix() {
int r = 0;
int c = 0;
int **p = NULL;
}
friend istream &operator>>(istream &o, Matrix &a) {
o >> a.r >> a.c;
int **p = new int*[a.r];
for (int i = 0; i < a.r; i++) {
p[i] = new int[a.c];
}
for (int i = 0; i < a.r; i++) {
for (int j = 0; j < a.c; j++) {
o >> a.p[i][j];
}
}
return o;
}
};
int main() {
Matrix a, b;
cin >> a >> b;
return 0;
}
求问各位大佬,我想通过运算符重载录入一个二维数组,但是编译时引发了异常: 读取访问权限冲突。
a.p 是 0x1110112 展开
展开全部
#include<iostream>
using namespace std;
class Matrix
{
public:
int ** p;
int r ;
int c ;
Matrix()
{
r = 0;
c = 0;
p = NULL;
}
~Matrix()
{
for (int i = 0; i < r; i++)
delete [] p[i];
delete [] p;
}
friend istream &operator>>(istream &o, Matrix &a)
{
o >> a.r >> a.c;
a.p = new int*[a.r];
for (int i = 0; i < a.r; i++)
{
a.p[i] = new int[a.c];
}
for (int i = 0; i < a.r; i++)
{
for (int j = 0; j < a.c; j++)
{
o >> a.p[i][j];
}
}
return o;
}
void output()
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Matrix a, b;
cin >> a >> b;
a.output();
cout<<endl;
b.output();
return 0;
}
using namespace std;
class Matrix
{
public:
int ** p;
int r ;
int c ;
Matrix()
{
r = 0;
c = 0;
p = NULL;
}
~Matrix()
{
for (int i = 0; i < r; i++)
delete [] p[i];
delete [] p;
}
friend istream &operator>>(istream &o, Matrix &a)
{
o >> a.r >> a.c;
a.p = new int*[a.r];
for (int i = 0; i < a.r; i++)
{
a.p[i] = new int[a.c];
}
for (int i = 0; i < a.r; i++)
{
for (int j = 0; j < a.c; j++)
{
o >> a.p[i][j];
}
}
return o;
}
void output()
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
Matrix a, b;
cin >> a >> b;
a.output();
cout<<endl;
b.output();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询