简体   繁体   中英

invalid use of 'this' outside of a non-static member function?

I wrote the following code inside the public part in my Matrix<T> class:

#include <iostream>
#include "Auxiliaries.h"

namespace mtm {
    template<class T>
    class Matrix {
    private:
        Dimensions dimensions;
        T *data;

    public:

        iterator_impl<T> begin(){};

        class AccessIllegalElement;

        class IllegalInitialization;

        class DimensionMismatch;

        Matrix(const Dimensions &matrix_dimensions, const T &initial_value = T());

    };


/**Iterators**/

    template<typename T>
    class iterator_impl;

    template<typename T>
    iterator_impl<T> begin(){
        return iterator(this, 0);
    }

    template<typename T>
    class iterator_impl{
    private:
        const Matrix<T> *matrix;
        int index;
        friend class Matrix<T>;

    public:
        iterator_impl(const iterator_impl &) = default;

        iterator_impl &operator=(const iterator_impl &) = default;

        ~iterator_impl() = default;

        iterator_impl(const Matrix<T> *matrix, int index)
                : matrix(matrix), index(index) {}

        iterator_impl &operator++()
        {
            ++index;
            return *this;
        }

        iterator_impl operator++(int)
        {
            iterator_impl result = *this;
            ++*this;
            return result;
        }

        bool operator==(const iterator_impl &it) const
        {
            return index == it.index;
        }

        bool operator!=(const iterator_impl &it) const
        {
            return !(*this == it);
        }

        T &operator*() const {
            return matrix->data[index];
        }

    };
    template<typename T>
    using iterator = iterator_impl<T>;
    template<typename T>
    using const_iterator = iterator_impl<const T>;

}

But I'm getting the following error:

invalid use of 'this' outside of a non-static member function
        return iterator(this, 0);

What did I do wrong here and how may I solve this one?

my class:

template<class T>
class Matrix {
private:
    Dimensions dimensions;
    T *data;

public:

    iterator_impl<T> begin(){};
    //....
}

https://wandbox.org/permlink/R4rQjGVNZWUHtMqj

What should this mean in this context?

template<typename T>
iterator_impl<T> begin(){
    return iterator(this, 0);
}

It should be a pointer to the object of the class the method belongs to, but the begin function is a free function that is not a member of any class.

How do you plan to use this function? Basic patterns are:

container.begin();

or

begin(container);

In both cases there is a container : the object of the class you have forgotten in your sample.

Update

According you to your updates, this should be the pointer to the Matrix object. You have already declared the method, not let's define it. The implementation is actually up to you, I'm not sure if it is correct. The key part is that you have forgotten to specify the class in the function signature:

template<typename T>
iterator_impl<T> Matrix<T>::begin() {
    return iterator(this, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM