| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1805 人关注过本帖
标题:发现问题,不懂,请教
只看楼主 加入收藏
浪里白条ing
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-3
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:6 
发现问题,不懂,请教
如图所示:为什么会发生异常?
外加问题:对数组指针怎么 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编辑过]

搜索更多相关主题的帖子: 如图所示 public include private default 
2017-05-21 16:31
浪里白条ing
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-3
收藏
得分:0 
2017-05-21 17:18
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:5 
#include <iostream>
#include <cstdlib>
//using namespace std;
using std::cout;using std::cin;using std::endl;
using std::ostream;using std::istream;

class FlatArray {//二维数组类
private:
    int *p;
    int col;
    int row;
public:
    //FlatArray() = default;
    FlatArray(int, int);
    istream& operator>>(istream& is);
    //ostream& operator<<(ostream& os);
    FlatArray& operator+(FlatArray&);
    FlatArray& operator-(FlatArray&);
    friend ostream&operator<<(ostream& os,const FlatArray&f);

};

FlatArray::FlatArray(int c, int r) :col(c), row(r) {
    p=new int[c+r-1];
    int i = 0, j = 0;
    while (i<col) {
        j=0;
        while (j < row) {
            p[i + j] = j + 10;
            j++;
        }
        i++;
    }
}


ostream& operator<<(ostream& os,const FlatArray&f) {
    int i = 0, j = 0;
    while (i < f.col) {
        j=0;
        while (j < f.row) {
            os<<f.p[j + i]<<' ';
            j++;
        }
        i++;
    }
    return os;
}
int main()
{
    FlatArray A(2, 3);
    cout<<A;
    system("pause");
    return 0;
}
2017-05-21 18:03
浪里白条ing
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-3
收藏
得分:0 
回复 3楼 yangfrancis
你给出了一种解决方案,表示感谢。
但是,还是没有解决我的问题。
2017-05-21 19:19
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
看不懂,既然定义了 int(*p)[3]; 那怎么又有 int col; ?列数不一样是3吗?

而且
FlatArray::FlatArray(int c, int r)里面的代码我也看不懂你想实现什么功能

程序代码:
#include <iostream>
#include <vector>

class FlatArray
{
public:
    FlatArray( size_t row, size_t col );
private:
    size_t colsize_;
    std::vector<int> buf_;

    friend std::istream& operator>>( std::istream& is, FlatArray& fa );
    friend std::ostream& operator<<( std::ostream& os, const FlatArray& fa );
};

FlatArray::FlatArray( size_t row, size_t col ) : colsize_(col), buf_(row*col)
{
}

std::istream& operator>>( std::istream& is, FlatArray& fa )
{
    for( size_t i=0; i!=fa.buf_.size(); ++i )
        if( !(is>> fa.buf_[i]) )
            break;
    return is;
}

std::ostream& operator<<( std::ostream& os, const FlatArray& fa )
{
    for( size_t i=0; i!=fa.buf_.size(); ++i )
        if( !(os << fa.buf_[i] << ((i+1)%fa.colsize_==0?'\n':' ')) )
            break;
    return os;
}

using namespace std;

int main( void )
{
    FlatArray a( 2, 3 );
    cin >> a;
    cout << a << endl;

    return 0;
}

2017-05-22 08:39
浪里白条ing
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-3
收藏
得分:0 
回复 5楼 rjsp
本来没有那个3,是调试的时候加的。
其实,我想问的是,如果类的某个数据成员是二维数组,除了以下方式(或类似)外,还有没有其他方式?
1.
程序代码:
const int a=2;
const int b=3;
class text{
    private:
        int c[a][b];
}

2.
程序代码:
class text{
    private:
        int *c;
        int a,b;
}


另外,发生异常的原因也请告知。
2017-05-22 12:11
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以下是引用浪里白条ing在2017-5-22 12:11:47的发言:

除了以下方式(或类似)外,还有没有其他方式?

二维数组的定义为 TYPE a[ROWSIZE][COLSIZE];
取其第row行第col列为 a[row][col]

假如用一维数组来模拟上面的二维数组,即 定义为 TYPE a[ROWSIZE*COLSIZE];
取其第row行第col列为 a[row*COLSIZE + col]


[此贴子已经被作者于2017-5-22 15:30编辑过]

2017-05-22 15:27
快速回复:发现问题,不懂,请教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025262 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved