Загрузка...

How to fix this crap?

Thread in C/C++ created by Muha665161 Oct 9, 2023. 115 views

  1. Muha665161
    Muha665161 Topic starter Oct 9, 2023 67 Dec 3, 2019
    C

    #include <iostream>
    #include <vector>
    #include <ctime>
    #include <stdexcept>


    template <typename T>

    class Image
    {
    int _width;
    int _height;
    std::vector<std::vector<T>> _data;
    public:
    Image(int width, int height, bool random) : _width(width), _height(height)
    {
    _data.resize(height, std::vector<T>(width, T(0)));
    if (random)
    {
    std::srand(std::time(nullptr));
    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    _data[i][j] = static_cast<T>(rand());
    }
    }
    }
    }

    T& operator()(int i, int j) const
    {
    if (i < 0 || i >= _width || j < 0 || j >= _height)
    throw std::out_of_range("Index out of range");
    return _data[j][i];
    }

    // Операторы умножения и сложения на константу
    Image<T> operator*(const T cnst) const
    {
    Image<T> result(_width, _height, false);

    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    T res = _data[i][j] * cnst;
    if (res < _data[i][j])
    result(i, j) = std::numeric_limits<T>::max();
    else
    result(i, j) = res;
    }
    }
    return result;
    }

    Image<T> operator+(const T cnst) const
    {
    Image<T> result(_width, _height, false);

    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    T res = _data[i][j] + cnst;
    if (res < _data[i][j])
    result(i, j) = std::numeric_limits<T>::max();
    else
    result(i, j) = res;
    }
    }
    return result;
    }

    // Операторы сложения и умножения для bool
    Image<bool> operator+(const Image<bool>& other) const
    {
    if (_width != other._width && _height != other._height)
    {
    throw std::invalid_argument("Размеры изображений не совпадают");
    }
    Image<bool> result(_width, _height, false);

    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    result(i, j) = _data[i][j] || other._data[i][j];
    }
    }
    return result;
    }

    Image<bool> operator*(const Image<bool>& other) const
    {
    if (_width != other._width && _height != other._height)
    {
    throw std::invalid_argument("Размеры изображений не совпадают");
    }
    Image<bool> result(_width, _height, false);

    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    result(i, j) = _data[i][j] && other._data[i][j];
    }
    }
    return result;
    }

    // Операторы сложения и умножения разных типов данных
    template <typename Q>
    Image<T> operator+(const Image<Q>& other) const
    {
    if (_width != other._width && _height != other._height)
    {
    throw std::invalid_argument("Размеры изображений не совпадают");
    }
    Image<T> result(_width, _height, false);
    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    T res = _data[i][j] + static_cast<T>(other._data[i][j]);
    if (res < _data[i][j])
    result(i, j) = std::numeric_limits<T>::max();
    else
    result(i, j) = res;
    }
    }
    }

    template <typename Q>
    Image<T> operator*(const Image<Q>& other) const
    {
    if (_width != other._width && _height != other._height)
    {
    throw std::invalid_argument("Размеры изображений не совпадают");
    }
    Image<T> result(_width, _height, false);
    for (int i = 0; i < _height; i++)
    {
    for (int j = 0; j < _width; j++)
    {
    T res = _data[i][j] * static_cast<T>(other._data[i][j]);
    if (res < _data[i][j])
    result(i, j) = std::numeric_limits<T>::max();
    else
    result(i, j) = res;
    }
    }
    }


    Image<T> operator!() const {
    Image<T> result(_width, _height, false);
    for (int i = 0; i < _height; ++i) {
    for (int j = 0; j < _width; ++j) {
    result(i, j) = !_data[i][j];
    }
    }
    return result;
    }

    };
    Ошибка возникает тут: [IMG].

    И Выглядит так: [IMG]
     
  2. Muha665161
    Muha665161 Topic starter Oct 9, 2023 67 Dec 3, 2019
  3. vtlstolyarov
    vtlstolyarov Oct 9, 2023 468 Jan 8, 2022
    Ну как бы логично - в векторе хранятся T а ты говоришь что вернёшь ссылку на T а возвращаешь сам T. Тут уж или крестик сними или трусы надень (в смысле или измени сигнатуру оператора чтобы возвращал T или возвращай ссылку на значение а не само значение)
     
  4. vtlstolyarov
    vtlstolyarov Oct 9, 2023 468 Jan 8, 2022
    UPD: Не, чёто я совсем подзабыл плюсы, тут без ста грамм не разберёшь. Что могу еще добавить это то что возвращать ссылку на элемент вектора может быть опасно, потому что если вектор ресайзнуть, то элементы переместятся и ссылка станет невалидной.
     
Loading...
Top