简体   繁体   English

在非静态成员 function 之外无效使用“this”?

[英]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:我在我的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:我的 class:

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

public:

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

https://wandbox.org/permlink/R4rQjGVNZWUHtMqj https://wandbox.org/permlink/R4rQjGVNZWUHtMqj

What should this mean in this context?在这种情况下, this意味着什么?

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. 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?你打算如何使用这个 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.在这两种情况下,都有一个container :您在样品中忘记了 class 的 object。

Update更新

According you to your updates, this should be the pointer to the Matrix object.根据您的更新, this应该是指向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:关键部分是您忘记在 function 签名中指定 class :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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