簡體   English   中英

出現錯誤:ISO C++ 禁止聲明沒有類型的“類型名稱”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM