标题:初学c++ 两矩阵相加 求助!!!
取消只看楼主
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
结帖率:100%
已结贴  问题点数:0 回复次数:2 
初学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
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
sun004715
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2010-10-30
得分:0 
回复 5楼 cymtl862327
谢谢  很有帮助
2010-11-07 20:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325153-1-1.html




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

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