假设我有一个模板类,我试图将其声明为朋友类.我应该转发声明类还是给它自己的模板?
例:
template <typename E>
class SLinkedList;
template <typename E>
class SNode {
private:
E elem;
SNode<E>* next;
friend class SLinkedList<E>;
};
Run Code Online (Sandbox Code Playgroud)
要么
template <typename E>
class SNode {
private:
E elem;
SNode<E>* next;
template <typename T>
friend class SLinkedList;
};
Run Code Online (Sandbox Code Playgroud) 我正在迭代一个2D数组,以使用索引值进行计算,然后将计算出的值分配给所述索引。
在NumPy文档中,提供了一个使用迭代器修改值的示例:
for x in np.nditer(a, op_flags=['readwrite']):
x[...] = 2 * x
Run Code Online (Sandbox Code Playgroud)
但是,使用以下方法跟踪索引时,这似乎不起作用:
it = np.nditer(a, flags=['multi_index'])
while not it.finished:
it[...] = . . .
it.iternext()
Run Code Online (Sandbox Code Playgroud)
但是,我可以使用这些it.multi_index值,但这似乎不必要。是否有更简单的方法可以通过不同的方法或不同的语法来实现?
it = np.nditer(a, flags=['multi_index'])
while not it.finished:
matrix[it.multi_index[0]][it.multi_index[1]] = . . .
it.iternext()
Run Code Online (Sandbox Code Playgroud)
编辑
这是一个multi_index尝试使用迭代器索引和失败来修改值的迭代示例。
matrix = np.zeros((5,5))
it = np.nditer(matrix, flags=['multi_index'])
while not it.finished:
it[...] = 1
it.iternext()
Run Code Online (Sandbox Code Playgroud)
产生的错误是
TypeError Traceback (most recent call last)
<ipython-input-79-3f4cabcbfde6> in <module>()
25 it = np.nditer(matrix, flags=['multi_index'])
26 …Run Code Online (Sandbox Code Playgroud) 该线程确实很有帮助,但我仍然对这个过程有疑问,但似乎没有得到解答。
我必须在多大程度上显式实例化模板?例如,如果在我的定义文件中,我在每个函数、友元类、运算符重载等上使用模板,我是否必须在模板实例化文件中实例化每个模板(我正在使用的当前方法)?
根据我的反复试验,答案似乎是否定的,一个简单的方法
template class Class<type>;
Run Code Online (Sandbox Code Playgroud)
将为班级的所有成员工作。然而,我已经阅读了其他建议的代码,并且将不胜感激具体的答案。