我试图在我的项目中使用来自子类的 typedef,我已经在下面的示例中隔离了我的问题.
I'm trying to use a typedef from a subclass in my project, I've isolated my problem in the example below.
有人知道我哪里出错了吗?
Does anyone know where I'm going wrong?
template<typename Subclass>
class A {
public:
//Why doesn't it like this?
void action(typename Subclass::mytype var) {
(static_cast<Subclass*>(this))->do_action(var);
}
};
class B : public A<B> {
public:
typedef int mytype;
B() {}
void do_action(mytype var) {
// Do stuff
}
};
int main(int argc, char** argv) {
B myInstance;
return 0;
}
这是我得到的输出:
sean@SEAN-PC:~/Documents/LucadeStudios/experiments$ g++ -o test test.cpp
test.cpp: In instantiation of ‘A<B>’:
test.cpp:10: instantiated from here
test.cpp:5: error: invalid use of incomplete type ‘class B’
test.cpp:10: error: forward declaration of ‘class B’
原因是在实例化类模板时,其成员函数的所有声明(而不是定义)也被实例化.当需要专门化的完整定义时,类模板被精确地实例化.例如,当它用作基类时就是这种情况,就像您的情况一样.
The reason is that when instantiating a class template, all its declarations (not the definitions) of its member functions are instantiated too. The class template is instantiated precisely when the full definition of a specialization is required. That is the case when it is used as a base class for example, as in your case.
那么发生的事情是 A 在
So what happens is that A<B> is instantiated at
class B : public A<B>
此时 B 还不是一个完整的类型(它在类定义的右大括号之后).但是A<B>::action的声明要求B是完整的,因为是在它的范围内爬行:
at which point B is not a complete type yet (it is after the closing brace of the class definition). However, A<B>::action's declaration requires B to be complete, because it is crawling in the scope of it:
Subclass::mytype
您需要做的是将实例化延迟到 B 完成的某个点.一种方法是修改 action 的声明,使其成为成员模板.
What you need to do is delaying the instantiation to some point at which B is complete. One way of doing this is to modify the declaration of action to make it a member template.
template<typename T>
void action(T var) {
(static_cast<Subclass*>(this))->do_action(var);
}
它仍然是类型安全的,因为如果 var 不是正确的类型,将 var 传递给 do_action 将会失败.
It is still type-safe because if var is not of the right type, passing var to do_action will fail.
这篇关于不完整类型的无效使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
std::reference_wrapper 和简单指针的区别?Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和简单指针的区别?)
常量之间的区别.指针和引用?Difference between const. pointer and reference?(常量之间的区别.指针和引用?)
c++ - 如何从指向向量的指针访问向量的内容?How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何从指向向量的指针访问向量的内容?)
*& 的含义和**&在 C++ 中Meaning of *amp; and **amp; in C++(*amp; 的含义和**amp;在 C++ 中)
为什么我不能对普通变量进行多态?Why can#39;t I do polymorphism with normal variables?(为什么我不能对普通变量进行多态?)
取消引用已删除的指针总是会导致访问冲突?Dereferencing deleted pointers always result in an Access Violation?(取消引用已删除的指针总是会导致访问冲突?)