C ++通用类错误,怎么了? [英] C++ generic class error, what's the problem?

查看:90
本文介绍了C ++通用类错误,怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下代码无法编译?

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

我得到错误:


'Matrix'不是类,名称空间或枚举

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

此外,如果在AccessIllegalElement内,我想编写一个打印非法索引的函数是什么?

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

1)定义一个带有一个参数的函数

1) to define a function that takes one parameter

2)给每个类对象一个称为索引的成员以保存该数据

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

推荐答案

Matrix 是模板,而不是类。在声明/定义内部项目时,需要让编译器知道此模板的模板参数:

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 {};

这篇关于C ++通用类错误,怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆