我想把矩阵转置、求逆、相乘等计算全部写在一个另外的类中,需要用时只要调用就行,但是MFC新手,不太理解,写出来的总是报错。比如矩阵转置,c的代码如下//矩阵转置
double *MatrixTranpose(double *A,double *B,int m,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
B[i*m+j]=A[j*n+i];
return B;
}但是放在MFC中,// matrix.cpp: implementation of the matrix class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "example.h"
#include "matrix.h"
#include "ModalDlg.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////matrix::matrix()
{}matrix::~matrix()
{}void matrix::MatrixTranpose(double *A,double *B,int m,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
B[i*m+j]=A[j*n+i];
return B;}
这么写就不对,能不能帮我改一下,指导一下这种情况怎么写代码?

解决方案 »

  1.   

    // matrix.h
    class matrix
    {
    ...
    public:
    double *MatrixTranpose(double *A,double *B,int m,int n);
    };//  matrix.cpp
    void matrix::MatrixTranpose(double *A,double *B,int m,int n) 
    {     
    int i,j;     
    for(i=0;i<n;i++)        
     for(j=0;j<m;j++)    
             B[i*m+j]=A[j*n+i];     
    return B;   

      

  2.   

    我是这么写的,但是会报错E:\example\matrix.cpp(33) : error C2556: 'void __thiscall matrix::MatrixTranpose(double *,double *,int,int)' : overloaded function differs only by return type from 'double *__thiscall matrix::MatrixTranpose(double *,double *,int,int)'
            e:\example\matrix.h(16) : see declaration of 'MatrixTranpose'
    E:\example\matrix.cpp(33) : error C2040: 'MatrixTranpose' : 'void (double *,double *,int,int)' differs in levels of indirection from 'double *(double *,double *,int,int)'
    执行 cl.exe 时出错.
    不太明白这里的错误
      

  3.   

    都是double*就对了 
      

  4.   


    class matrix
    {
    double *p;
    int m_row,m_col;
    void init(int row,int col);
    public:
    //matrix(const matrix&);
    //......matrix MatrixTranpose();matrix(int row,int col);
    matrix(double *a,int row,int col);~matrix();int rows ()const {return m_row;};
    int cols()const{return m_col;};};
    void matrix::init(int row,int col){
    try{ASSERT(row>0);
    ASSERT(c>0);
    if(row>0 && col) p = new double[row * col];
    }catch(...)
    {}
    m_row = row;
    m_col = col;
    }
    matrix:: matrix (double *a,int r,int c):p(NULL){
    init(r,c);
    memcpy(p,a,sizeof(double) *row * col );
    }
    matrix ::matrix(int r,int c):p(NULL){
    init(r,c);
    };
    matrix::~matrix(){delete []p;}matrix matrix ::MatrixTranpose(){
    int row =rows(),col=cols();
    matrix mat(col,row);
     int i,j;     
    for(i=0;i<col;i++)        
     for(j=0;j<row;j++)    
             mat.p[i*col +j] = p[j *row + i];        
    return mat;
    }这才略有点,C++的矩阵的味道。