C++ generic class error, what's the problem?

丶灬走出姿态 提交于 2020-06-29 06:43:11

问题


Why the following code doesn't compile?

namespace mtm {
    template<class T>
    class Matrix {
    private:
    public:
        class AccessIllegalElement;

    };

    Matrix::AccessIllegalElement{};
}

I'm trying to implement the inner class for handling errors

Error I get:

'Matrix' is not a class, namespace, or enumeration

Plus, if inside AccessIllegalElement I want to write a function that prints the illegal index what is preferable?

1) to define a function that takes one parameter

2) to give every class object a member called index to save that data


回答1:


Matrix is a template, not a class. You need to let the compiler know the template arguments of this template when you declare/define inner items:

template <typename T>
class Matrix<T>::AccessIllegalElement {};


来源:https://stackoverflow.com/questions/62359649/c-generic-class-error-whats-the-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!