| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3475 人关注过本帖
标题:初学c++ 两矩阵相加 求助!!!
只看楼主 加入收藏
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:6 
初学c++ 两矩阵相加 求助!!!
题目: 有两个矩阵 a 和 b ,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c = a + b。

我编的程序如下 请大牛 帮忙改改:
/*
 * Array.cpp
 *
 *  Created on: 2010-11-5
 *      Author: sun
 */

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex()
    {
        int a[2][3] = {{0, 0, 0}, {0, 0, 0}};
    }

    Complex(int m[2][3])
    {
        a[2][3] = m[2][3];
    }
    void display(void);
    Complex operator + (Complex&);
private:
    int a[2][3];
};

Complex Complex :: operator + (Complex &c1)
        {
            int temp[2][3];
            for (int i=0; i<2; i++)
                for (int j=0; i<3; i++)
                {
                    temp[2][3] =(a[i][j] + c1.a[i][j]);
                }
            return Complex(temp[2][3]);
        }

void Complex :: display(void)
{
    cout << a[2][3];
}

int main(void)
{
    int member1[2][3] = {{1, 1, 1}, {1, 1, 1}};
    int member2[2][3] = {{2, 2, 2}, {2, 2, 2}};

    Complex a(member1[2][3]), b(member2[2][3]), c;
    c = a + b;
    c.display();
}


搜索更多相关主题的帖子: 矩阵 相加 初学 
2010-11-06 11:14
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:3 
    Complex(int m[2][3])
    {
        a[2][3] = m[2][3];
    }
这个定义不行吧!!!(a[2][3] = m[2][3];能达到什么目的?)

★★★★★为人民服务★★★★★
2010-11-06 17:38
lurrass
Rank: 1
等 级:新手上路
帖 子:17
专家分:1
注 册:2010-9-13
收藏
得分:0 
#include <iostream>
using namespace std;

class Complex
{
public:
    Complex()
    {
        int a[2][3] = {{0, 0, 0}, {0, 0, 0}};
    }

    Complex(int m[2][3])
    {
        a[2][3] = m[2][3];
    }
    void display(void);
    Complex operator + (Complex&);
private:
    int a[2][3];
};

Complex Complex :: operator + (Complex &c1)
        {
            int temp[2][3];
            for (int i=0; i<2; i++)
                for (int j=0; i<3; i++)
                {
                    temp[2][3] =(a[i][j] + c1.a[i][j]);
                }
            return Complex(temp[2][3]);
        }

void Complex :: display(void)
{
    cout << a[2][3];
}

int main(void)
{
    int member1[2][3] = {{1, 1, 1}, {1, 1, 1}};
    int member2[2][3] = {{2, 2, 2}, {2, 2, 2}};

    Complex a(member1[2][3]), b(member2[2][3]), c;
    c = a + b;
    c.display();
}

2010-11-06 19:29
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
收藏
得分:0 
谢谢cnfarer !! 我把那儿改了 但后面还是有错
/*
 * Array.cpp
 *
 *  Created on: 2010-11-5
 *      Author: sun
 */

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex()
    {
        int a[2][3] = {{0, 0, 0}, {0, 0, 0}};
    }

    Complex(int m[2][3])
    {
        for (int i=0; i<2; i++)
            for (int j=0; i<3; i++)
        a[i][j] = m[i][j];
    }
    void display(void);
    Complex operator + (Complex&);
private:
    int a[2][3];
};

Complex Complex :: operator + (Complex &c1)
        {
            int temp[2][3];
            for (int i=0; i<2; i++)
                for (int j=0; i<3; i++)
                {
                    temp[2][3] =(a[i][j] + c1.a[i][j]);
                }
            return Complex(temp[2][3]);//这句错 ..\Array.cpp:39: error: invalid conversion from `int' to `int (*)[3]'
                                                //..\Array.cpp:39: error:   initializing argument 1 of `Complex::Complex(int (*)[3])'
        }

void Complex :: display(void)
{
    cout << a[2][3];
}

int main(void)
{
    int member1[2][3] = {{1, 1, 1}, {1, 1, 1}};
    int member2[2][3] = {{2, 2, 2}, {2, 2, 2}};

    Complex a(member1[2][3]), b(member2[2][3]), c;//这句错
                                                    //..\Array.cpp:52: error: invalid conversion from `int' to `int (*)[3]'
                                                    //..\Array.cpp:52: error:   initializing argument 1 of `Complex::Complex(int (*)[3])'
    c = a + b;
    c.display();
}


2010-11-06 21:54
cymtl862327
Rank: 2
等 级:论坛游民
帖 子:16
专家分:27
注 册:2010-11-5
收藏
得分:2 
看你给的提示,应该是函数的参数有错吧!?
定义构造函数Complex(int m[2][3])时,形参是一个整形数组,而在调用含参构造函数( Complex(temp[2][3]);Complex a(member1[2][3]), b(member2[2][3]) )时传入的实参只是一个整数。
看看多维数组(整体)引用这一块吧,看有帮助么!?
呵呵、、
2010-11-07 00:16
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
收藏
得分:0 
回复 5楼 cymtl862327
谢谢  很有帮助
2010-11-07 20:01
jch8528563
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-11-8
收藏
得分:0 
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#define X 3
#define Y 3

int a[X][Y];
int b[X][Y];
int c[X][Y];

void matrix(int b[][X],int c[][Y]);
main()
{
    int i,j,temp;
    //clrscr();
    system("cls");
    printf("Please input int matrix b[%d][%d]\n",X,Y);
    for(i=0;i<Y;i++)
        for(j=0;j<Y;j++)
        {
            scanf("%d",&temp);
            b[i][j]=temp;
        }
        printf("Please input int matrix c[%d][%d]\n",X,Y);
        for(i=0;i<X;i++)
            for(j=0;j<Y;j++)
            {
                scanf("%d",&temp);
                c[i][j]=temp;
            }
            matrix(b,c);
            printf("Now print resource matrix b[%d][%d]=",X,Y);
            for(i=0;i<X;i++)
            {
                printf("\n");
                for(j=0;j<Y;j++)
                    printf("%d ",b[i][j]);
            }
            printf("\n");
            printf("Now print resource matrix c[%d][%d]=",X,Y);
            for(i=0;i<X;i++)
            {
                printf("\n");
                for(j=0;j<Y;j++)
                    printf("%d ",c[i][j]);
            }
            printf("\n");
            printf("Now printm multiply results matrix a[%d][%d]=B*C:",X,Y);
            for(i=0;i<X;i++)
            {
                printf("\n");
                for(j=0;j<Y;j++)
                    printf("%d ",a[i][j]);
            }
            getch();
            return 0;
}
/********************************************************************/
void matrix(int b[][X],int c[][Y])
{
    int i,j,k,temp;
    for(i=0;i<X;i++)
        for(j=0;j<Y;j++)
        {
            for(k=0;k<Y;k++)
                a[i][j]+=b[i][k]*c[k][j];
        }
}

2010-11-08 11:33
快速回复:初学c++ 两矩阵相加 求助!!!
数据加载中...
 
   



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

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