简体   繁体   English

出现错误:ISO C++ 禁止声明没有类型的“类型名称”

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

I have a nested class definition and get errors on applying a cast to the pointer to it.我有一个嵌套的 class 定义,并且在对指向它的指针应用强制转换时出错。 The following program compiles with the error:以下程序编译时出现错误:

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]
      }

I can't understand why i am getting errors on line 24. Any help would be appreciated!我不明白为什么我在第 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);
}

You are missing a typename declaration:您缺少typename声明:

ShortName::ptr = (const typename ShortName::Ptr*) input;

as ShortName depends on your template type.因为ShortName取决于您的模板类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM