我来我网
https://5come5.cn
 
您尚未 登录  注册 | 菠菜 | 软件站 | 音乐站 | 邮箱1 | 邮箱2 | 风格选择 | 更多 » 
 

本页主题: 哪位大人帮我看看这个程序的阿 显示签名 | 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

seast



性别: 帅哥 状态: 该用户目前不在线
等级: 人见人爱
发贴: 2806
威望: 0
浮云: 1585
在线等级:
注册时间: 2006-11-05
最后登陆: 2010-03-12

5come5帮你背单词 [ snob /snob/ n. 势利小人 ]


哪位大人帮我看看这个程序的阿

帮我看看这个程序哪个地方出错了!
老师说,对象矩阵没有初始化,可是我还是没有明白!谢谢阿

# include <iostream>
# include <cmath>

using namespace std;

class matrix
{
public :
    matrix (int dims);
    ~matrix();
    setmatrix(double *a);
    printM();
private:
    int index;
    double *matrixA;
};

matrix ::matrix(int dims)
{
    int index =dims;
    matrixA = new double[index * index];
}

matrix ::~matrix()
{
    delete [] matrixA;
}

matrix ::setmatrix (double *a )
{
    for (int i=0; i< index * index ; i++)
            *(matrixA + i) =  a [ i ];
}

matrix ::printM()
{
    cout << " the  matrix is "  << endl ;
    for (int i=0;i<index ;i++)
        for (int j=0;j < index ;j++)
            cout << * (matrixA + i* index +j)  << " " ;
            cout << endl ;
}


void main ()

    matrix matrix1(2);
    double a[]= {1.0, 2.0, 3.0, 4.0 } ;
    matrix1.setmatrix(a);
    matrix1.printM ( );
}


[ 此帖被seast在2007-11-30 16:48重新编辑 ]
顶端 Posted: 2007-11-30 15:59 | [楼 主]
yunyue



性别: 帅哥 状态: 该用户目前不在线
等级: 版主
发贴: 979
威望: 5
浮云: 407
在线等级:
注册时间: 2006-06-18
最后登陆: 2012-06-08

5come5帮你背单词 [ performance /pə'fo:məns/ n. 履行,完成,演出,表演 ]


//修改后没问题了
# include <iostream>
# include <cmath>

using namespace std;

class matrix
{
public :
    matrix (int dims);
    ~matrix();
    void setmatrix(double *a);          //这里修改,加上void
    void printM();                            //这里修改,加上void
private:
    int index;
    double *matrixA;
};

matrix ::matrix(int dims)
{
    index = dims;                  //这里修改,多了 int
    matrixA = new double[index * index];
}

matrix ::~matrix()
{
    delete [] matrixA;
}

void matrix ::setmatrix (double *a)
{
    for (int i=0; i< index * index ; i++){
            *(matrixA+i) = *(a+i);        //这也改了一下,原来的也没问题
    }           
}

void matrix ::printM()
{
    cout << " the  matrix is "  << endl ;
    for (int i=0;i<index ;i++){
        for (int j=0;j < index ;j++){
            cout << * (matrixA + i* index +j)  << " " ;
        }
        cout << endl ;
    }        //加了一对{}
}


int main ()          //这里修改过
{
    matrix matrix1(2);
    double a[]= {1.0, 2.0, 3.0, 4.0 } ;
    matrix1.setmatrix(a);
    matrix1.printM();        //这里修改过,少了()
    return 0;                    //加了一条语句
}


[ 此帖被yunyue在2007-11-30 16:56重新编辑 ]
顶端 Posted: 2007-11-30 16:51 | [1 楼]
yunyue



性别: 帅哥 状态: 该用户目前不在线
等级: 版主
发贴: 979
威望: 5
浮云: 407
在线等级:
注册时间: 2006-06-18
最后登陆: 2012-06-08

5come5帮你背单词 [ contempt /kən'tempt/ n. 轻视,蔑视,受辱,丢脸 ]


int index = dims;
这样的话,index就是局部变量
顶端 Posted: 2007-11-30 17:01 | [2 楼]
我来我网·5come5 Forum » 程序员之家

Total 0.010754(s) query 6, Time now is:07-04 18:58, Gzip enabled
Powered by PHPWind v5.3, Localized by 5come5 Tech Team, 黔ICP备16009856号