[英]Getting error: ISO C++ forbids declaration of ‘type name’ with no type
我有一个嵌套的 class 定义,并且在对指向它的指针应用强制转换时出错。 以下程序编译时出现错误:
test.cpp: In member function ‘void* Achild<T>::test(void*)’:
test.cpp:24:31: error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
ShortName::ptr = (const ShortName::Ptr*)input;
^~~~~~~~~
test.cpp:24:25: error: expected primary-expression before ‘const’
ShortName::ptr = (const ShortName::Ptr*)input;
^~~~~
test.cpp:24:25: error: expected ‘)’ before ‘const’
ShortName::ptr = (const ShortName::Ptr*)input;
~^~~~~
)
test.cpp:25:6: warning: no return statement in function returning non-void [-Wreturn-type]
}
我不明白为什么我在第 24 行遇到错误。任何帮助将不胜感激!
template<typename T>
class VeryLongName
{
public:
class Ptr
{
public:
int a;
Ptr() = default;
};
const Ptr* ptr;
};
template <typename T>
class Achild: public VeryLongName<T>
{
using ShortName = VeryLongName<T>;
public:
Achild<T>() = default;
void test(void * input)
{
ShortName::ptr = (const ShortName::Ptr*)input;
}
};
int main()
{
auto *achild = new Achild<int>();
auto a = new VeryLongName<int>::Ptr();
achild->test((void*)a);
}
您缺少typename
声明:
ShortName::ptr = (const typename ShortName::Ptr*) input;
因为ShortName
取决于您的模板类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.