BlinCT
BlinCTApril 3, 2017, 5:07 a.m.

Перегрузка оператор + для класса матрицы

C++

Всем привет. Вопрос состоит в реализации перегрузки оператора. Думаю что я что то не правильно указываю так как класс шаблонный а с этим опыта не много. Ниже укажу хедер класса и реализацию, надеюсь кто то подскажет что не так у меня в коде. CustomMatrix.hpp

#pragma once

#include <QVector>


template <typename T>
friend CustomMatrix<T> &operator+(const CustomMatrix &a, const CustomMatrix &b);
template <typename T>
friend CustomMatrix &operator*(const CustomMatrix &a, const CustomMatrix &b);

template <typename T>
class CustomMatrix
{
public:
    //CustomMatrix();
    CustomMatrix<T> TopLeftCorner(int i, int j);
    CustomMatrix(const CustomMatrix &) = default;
    CustomMatrix(CustomMatrix &&) = default;

    CustomMatrix(int rowSize, int colSize);

    const T at(int x, int y) const;
    T & at(int x, int y);

    ~CustomMatrix() = default;
private:
    int m_iRowSize;
    int m_iColSize;

    QVector<T> m_arrVector;
};

#include "CustomMatrix_impl.hpp"
А тут уже реализация перегрузки: CustomMatrix_impl.hpp
template<typename T>
CustomMatrix &operator+(const CustomMatrix &a, const CustomMatrix &b)
{
    CustomMatrix c;

    if(a.m_iColSize == b.m_iColSize && a.m_iRowSize == b.m_iRowSize)
    {
        for (int i = 0; i < a.m_iRowSize; ++i)
        {
            for (int j = 0; j < a.m_iColSize; ++j)
            {
                c.at(i, j) = a.at(i, j) + b.at(i, j);
            }
        }
    }
    return c;
}

template<typename T>
CustomMatrix &operator*(const CustomMatrix &a, const CustomMatrix &b)
{
    CustomMatrix c;

    if(a.m_iColSize == b.m_iRowSize)
    {
        for (int i = 0; i < a.m_iColSize; ++i)
        {
            for (int j = 0; j < a.m_iRowSize; ++j)
            {
                T sum = 0;
                for (int k = 0; k < a.m_iColSize; ++k)
                {
                    sum = sum + a.at(i, k) * b.at(k, j);
                }
                c.at(i, j) = sum;
            }
        }
    }
    return c;
}
We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

2
BlinCT
  • April 3, 2017, 6:29 a.m.

Переделал все без шаблона хедер:

friend CustomMatrix &operator+(const CustomMatrix &a, const CustomMatrix &b);
friend CustomMatrix &operator*(const CustomMatrix &a, const CustomMatrix &b);

class CustomMatrix
{
public:
    //CustomMatrix();
    CustomMatrix<T> TopLeftCorner(int i, int j);
    CustomMatrix(const CustomMatrix &) = default;
    CustomMatrix(CustomMatrix &&) = default;

    CustomMatrix(int rowSize, int colSize);

    const double at(int x, int y) const;
    double & at(int x, int y);

    ~CustomMatrix() = default;
private:
    int m_iRowSize;
    int m_iColSize;

    QVector<double> m_arrVector;
};
И сама реализация:
CustomMatrix &operator+(const CustomMatrix &a, const CustomMatrix &b)
{
    CustomMatrix c(m_iRowSize, m_iColSize);

    if(a.m_iColSize == b.m_iColSize && a.m_iRowSize == b.m_iRowSize)
    {
        for (int i = 0; i < a.m_iRowSize; ++i)
        {
            for (int j = 0; j < a.m_iColSize; ++j)
            {
                c.at(i, j) = a.at(i, j) + b.at(i, j);
            }
        }
    }
    return c;
}

CustomMatrix &operator*(const CustomMatrix &a, const CustomMatrix &b)
{
    CustomMatrix c;

    if(a.m_iColSize == b.m_iRowSize)
    {
        for (int i = 0; i < a.m_iColSize; ++i)
        {
            for (int j = 0; j < a.m_iRowSize; ++j)
            {
                T sum = 0;
                for (int k = 0; k < a.m_iColSize; ++k)
                {
                    sum = sum + a.at(i, k) * b.at(k, j);
                }
                c.at(i, j) = sum;
            }
        }
    }
    return c;
}
    Evgenii Legotckoi
    • April 3, 2017, 6:35 a.m.
    • The answer was marked as a solution.

    День добрый.
    Думаю, что объявление friend стоит поместить внутрь класса.

    class CustomMatrix
    {
    public:
        //CustomMatrix();
        CustomMatrix<T> TopLeftCorner(int i, int j);
        CustomMatrix(const CustomMatrix &) = default;
        CustomMatrix(CustomMatrix &&) = default;
    
        CustomMatrix(int rowSize, int colSize);
    
        friend CustomMatrix &operator+(const CustomMatrix &a, const CustomMatrix &b)
        {
            CustomMatrix c(m_iRowSize, m_iColSize);
    
            if(a.m_iColSize == b.m_iColSize && a.m_iRowSize == b.m_iRowSize)
            {
                for (int i = 0; i < a.m_iRowSize; ++i)
                {
                    for (int j = 0; j < a.m_iColSize; ++j)
                    {
                        c.at(i, j) = a.at(i, j) + b.at(i, j);
                    }
                }
            }
            return c;
        }
    
        friend CustomMatrix &operator*(const CustomMatrix &a, const CustomMatrix &b)
        {
            CustomMatrix c;
    
            if(a.m_iColSize == b.m_iRowSize)
            {
                for (int i = 0; i < a.m_iColSize; ++i)
                {
                    for (int j = 0; j < a.m_iRowSize; ++j)
                    {
                        T sum = 0;
                        for (int k = 0; k < a.m_iColSize; ++k)
                        {
                            sum = sum + a.at(i, k) * b.at(k, j);
                        }
                        c.at(i, j) = sum;
                    }
                }
            }
            return c;
        }
    
        const double at(int x, int y) const;
        double & at(int x, int y);
    
        ~CustomMatrix() = default;
    private:
        int m_iRowSize;
        int m_iColSize;
    
        QVector<double> m_arrVector;
    };

      Comments

      Only authorized users can post comments.
      Please, Log in or Sign up
      AD

      C ++ - Test 004. Pointers, Arrays and Loops

      • Result:50points,
      • Rating points-4
      m

      C ++ - Test 004. Pointers, Arrays and Loops

      • Result:80points,
      • Rating points4
      m

      C ++ - Test 004. Pointers, Arrays and Loops

      • Result:20points,
      • Rating points-10
      Last comments
      i
      innorwallNov. 11, 2024, 10:12 p.m.
      Django - Tutorial 055. How to write auto populate field functionality Freckles because of several brand names retin a, atralin buy generic priligy
      i
      innorwallNov. 11, 2024, 6:23 p.m.
      QML - Tutorial 035. Using enumerations in QML without C ++ priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medici…
      i
      innorwallNov. 11, 2024, 3:50 p.m.
      Qt/C++ - Lesson 052. Customization Qt Audio player in the style of AIMP It decreases stress, supports hormone balance, and regulates and increases blood flow to the reproductive organs buy priligy online safe Promising data were reported in a PDX model re…
      i
      innorwallNov. 11, 2024, 2:19 p.m.
      Heap sorting algorithm The role of raloxifene in preventing breast cancer priligy precio
      i
      innorwallNov. 11, 2024, 1:55 p.m.
      PyQt5 - Lesson 006. Work with QTableWidget buy priligy 60 mg 53 have been reported by Javanovic Santa et al
      Now discuss on the forum
      i
      innorwallNov. 11, 2024, 8:56 p.m.
      добавить qlineseries в функции buy priligy senior brother Chu He, whom he had known for many years
      i
      innorwallNov. 11, 2024, 10:55 a.m.
      Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
      9
      9AnonimOct. 25, 2024, 9:10 a.m.
      Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

      Follow us in social networks