gpt4 book ai didi

c++ - 出现错误 : ISO C++ forbids declaration of ‘type name’ with no type

转载 作者:行者123 更新时间:2023-12-02 00:29:06 26 4
gpt4 key购买 nike

我有一个嵌套类定义,并且在将强制转换应用于指向它的指针时出现错误。以下程序编译时出现错误:

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;

as ShortName 取决于您的模板类型。

关于c++ - 出现错误 : ISO C++ forbids declaration of ‘type name’ with no type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61376226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com