注册 登录
编程论坛 C++教室

C++的问题,求大侠解释(老师也没说明白)

hchhbbcxl 发布于 2011-04-11 20:29, 389 次点击
#include <conio.h>
#include <memory.h>
#include <iostream>
#include "stdio.h"
using namespace std;

class matrix{
    short rows, cols;
    int *elems;
public:
    matrix(short rows, short cols);        // 构造函数
    ~matrix();                            // 析构函数

    // 重载'[]'以支持下标操作
    int* operator[] (short row);

    // 添加友元函数打印矩阵
    friend void PrintMatrix(matrix m);


     matrix(const matrix & p);            // 拷贝构造函数

     void operator = (matrix p);            // 重载'='实现矩阵之间的赋值
};


//---------------------------------------------类实现----------------------------------------------
// 构造函数,分配内存
matrix::matrix(short rows, short cols)
{
    matrix::rows = rows;
    matrix::cols = cols;
    elems = new int[rows * cols];

    // 初始化所有值为0
    memset(elems, 0, sizeof(int) * rows * cols);
}

// 析构函数,释放内存

matrix::~matrix(){
    if( elems != NULL){
        delete []elems;
        elems = NULL;
    }
}

// 重载'[]'以支持下标运算
int* matrix::operator[](short row)
{
    // 逻辑上,在类matrix的外部使用者看来它是一个二维矩阵,
    // 所以'[]'运算应该返回一个一维数组的首地址,其实是个指针
    return elems + row * cols;
}

// 输出矩阵各元素的值
void PrintMatrix(matrix m)
{
    for(int i = 0; i < m.rows; i++){
        for(int k = 0; k < m.cols; k++){
//           cout << m.elems[i * m.cols + k] << "  ";
            
        }
//        cout << endl;
    }
//    cout << endl;

}
   
// 重写拷贝构造函数
matrix::matrix(const matrix & p)
{
    rows = p.rows;
    cols = p.cols;
    elems = new int[p.rows * p.cols];
    memcpy( elems, p.elems, sizeof(double) * p.rows * p.cols);
}

// 重载'='实现矩阵之间的赋值
void matrix::operator = (matrix p)
{
    rows = p.rows;
    cols = p.cols;
    elems = new int[p.rows * p.cols];
    memcpy( elems, p.elems, sizeof(double) * p.rows * p.cols);
}

int main(int argc, char* argv[])
{
    matrix  a(2,3);

    PrintMatrix(a);

    a[0][0] = 1;
    a[0][1] = 2;
    a[0][2] = 3;
    a[1][0] = 4;
    a[1][1] = 5;
    a[1][2] = 6;

    PrintMatrix(a);

    getch();
    return 0;
}


上面的代码用F5可以运行,但使用Ctrl+F5却出现了XX遇到问题需要关闭,求解释。。。
8 回复
#2
寒风中的细雨2011-04-11 21:48
程序代码:
#include <iostream>
#include <cstdio>
#include <memory>
#include <windows.h>
using namespace std;

class matrix
{
    int *m_elems;
    short m_rows, m_cols;

public:
    matrix(short rows, short cols);
    ~matrix();
    int *operator[] (short row);
    friend void PrintMatrix(matrix m);
    matrix(const matrix &p);
    void operator = (matrix p);
};

matrix::matrix(short rows, short cols)
{
    m_rows = rows;
    m_cols = cols;
    m_elems = new int[rows*cols];

    memset(m_elems, 0, sizeof(int)*rows*cols);
}

matrix::~matrix()
{
    if (NULL != m_elems)
    {
        delete [] m_elems;
        m_elems = NULL;
    }
}

int * matrix::operator [](short row)
{
    static i = 0;
    cout << ++i << endl;
    return m_elems + row * m_cols;
}

void PrintMatrix(matrix m)
{
    for (int i=0; i<m.m_rows; ++i)
    {
        for (int k=0; k < m.m_cols; ++k)
        {
            cout << m.m_elems[i*m.m_cols+k] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

matrix::matrix(const matrix &p)
{
    m_rows = p.m_rows;
    m_cols = p.m_cols;
    m_elems = new int[p.m_cols*p.m_rows];

    memcpy(m_elems, p.m_elems, sizeof(int)*p.m_cols*p.m_rows);
}

void matrix::operator =(matrix p)
{
    m_rows = p.m_rows;
    m_cols = p.m_cols;
    m_elems = new int[p.m_cols*p.m_rows];

    memcpy(m_elems, p.m_elems, sizeof(int)*p.m_cols*p.m_rows);
}

int main()
{
    matrix a(2, 3);

    a[0][0] = 1;
    a[0][1] = 2;
    a[0][2] = 3;
    a[1][0] = 4;
    a[1][1] = 5;
    a[1][2] = 6;



    PrintMatrix(a);

    system("pause");

    return 0;
}
#3
寒风中的细雨2011-04-11 21:49
试下这样子的
#4
寒风中的细雨2011-04-11 21:51
你的程序 在我的vc上没有出现你说的情况
#5
hchhbbcxl2011-04-11 22:17
回复 4楼 寒风中的细雨
不会吧,但在老师的VC上也一样,而且我用VC2008也一样,好,我试下你写的,但我还想知道为什么用Ctrl+F5运行不了
#6
hchhbbcxl2011-04-11 22:24
回复 4楼 寒风中的细雨
你直接复制我的程序也能正常用Ctrl+F5运行吗?
#7
寒风中的细雨2011-04-11 22:32
可以
#8
hchhbbcxl2011-04-11 22:36
==我发现了, memcpy( elems, p.elems, sizeof(double) * p.rows * p.cols);
memcpy(m_elems, p.m_elems, sizeof(int)*p.m_cols*p.m_rows);
应该是sizeof(int),但为什么用F5运行我上述代码的时候还是能得到结果呢?
#9
玩出来的代码2011-04-11 22:48
release、、、、
1