发现问题,不懂,请教
如图所示:为什么会发生异常?外加问题:对数组指针怎么 new 空间呢?
[local] [/local]
源码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
class FlatArray {//二维数组类
public:
//FlatArray() = default;
FlatArray(int, int);
istream& operator>>(istream& is);
ostream& operator<<(ostream& os);
FlatArray& operator+(FlatArray&);
FlatArray& operator-(FlatArray&);
private:
int(*p)[3];
int col;
int row;
};
FlatArray::FlatArray(int c, int r) :col(c), row(r) {
int i = 0, j = 0;
while (i<col) {
while (j < row) {
(*p)[i + j] = j + 10;
j++;
}
i++;
}
}
/*istream& FlatArray::operator >> (istream& is) {
cout << "请输入六个数字\n";
}*/
ostream& FlatArray::operator<<(ostream& os) {
int i = 0, j = 0;
while (i < col) {
while (j < row) {
os << (*p)[j + i]<<' ';
j++;
}
i++;
}
return os;
}
int main()
{
FlatArray A(2, 3);
system("pause");
return 0;
}
[此贴子已经被作者于2017-5-21 16:32编辑过]