C ++通用类错误,怎么了? [英] C++ generic class error, what's the problem?
问题描述
为什么以下代码无法编译?
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屋!