我有一个嵌套的类定义,并且在对指向它的指针应用强制转换时出错。以下程序编译时出现错误:
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]
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我在第 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);
}
Run Code Online (Sandbox Code Playgroud)
您缺少typename
声明:
ShortName::ptr = (const typename ShortName::Ptr*) input;
Run Code Online (Sandbox Code Playgroud)
因为ShortName
取决于你的模板类型。
归档时间: |
|
查看次数: |
993 次 |
最近记录: |