获取错误:ISO C++禁止声明没有类型的"类型名称"

Getting error: ISO C++ forbids declaration of ‘type name’ with no type

本文关键字:类型 quot 取错误 ISO C++ 声明 禁止 获取      更新时间:2024-03-28

我有一个嵌套的类定义,在对指向它的指针应用强制转换时出错

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取决于您的模板类型。