久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

不完整類型的無效使用

invalid use of incomplete type(不完整類型的無效使用)
本文介紹了不完整類型的無效使用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我試圖在我的項目中使用來自子類的 typedef,我已經在下面的示例中隔離了我的問題.

I'm trying to use a typedef from a subclass in my project, I've isolated my problem in the example below.

有人知道我哪里出錯了嗎?

Does anyone know where I'm going wrong?

template<typename Subclass>
class A {
    public:
        //Why doesn't it like this?
        void action(typename Subclass::mytype var) {
            (static_cast<Subclass*>(this))->do_action(var);
        }
};

class B : public A<B> {
    public:
        typedef int mytype;

        B() {}

        void do_action(mytype var) {
            // Do stuff
        }
};

int main(int argc, char** argv) {
    B myInstance;
    return 0;
}

這是我得到的輸出:

sean@SEAN-PC:~/Documents/LucadeStudios/experiments$ g++ -o test test.cpp
test.cpp: In instantiation of ‘A<B>’:
test.cpp:10:   instantiated from here
test.cpp:5: error: invalid use of incomplete type ‘class B’
test.cpp:10: error: forward declaration of ‘class B’

推薦答案

原因是在實例化類模板時,其成員函數的所有聲明(而不是定義)也被實例化.當需要專門化的完整定義時,類模板被精確地實例化.例如,當它用作基類時就是這種情況,就像您的情況一樣.

The reason is that when instantiating a class template, all its declarations (not the definitions) of its member functions are instantiated too. The class template is instantiated precisely when the full definition of a specialization is required. That is the case when it is used as a base class for example, as in your case.

那么發生的事情是 A

So what happens is that A<B> is instantiated at

class B : public A<B>

此時 B 還不是一個完整的類型(它在類定義的右大括號之后).但是A<B>::action的聲明要求B是完整的,因為是在它的范圍內爬行:

at which point B is not a complete type yet (it is after the closing brace of the class definition). However, A<B>::action's declaration requires B to be complete, because it is crawling in the scope of it:

Subclass::mytype

您需要做的是將實例化延遲到 B 完成的某個點.一種方法是修改 action 的聲明,使其成為成員模板.

What you need to do is delaying the instantiation to some point at which B is complete. One way of doing this is to modify the declaration of action to make it a member template.

template<typename T>
void action(T var) {
    (static_cast<Subclass*>(this))->do_action(var);
}

它仍然是類型安全的,因為如果 var 不是正確的類型,將 var 傳遞給 do_action 將會失敗.

It is still type-safe because if var is not of the right type, passing var to do_action will fail.

這篇關于不完整類型的無效使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产一区二区日韩 | 九九av| 国产精品中文字幕在线播放 | 91视视频在线观看入口直接观看 | 999久久久久久久久6666 | 亚洲精品久久嫩草网站秘色 | 亚洲一二三区在线观看 | 欧美国产日韩在线观看成人 | 欧美一级大片免费看 | 欧美久久大片 | 免费在线黄色av | 国产精品99久久久久久宅男 | 先锋资源网 | 国产精品久久久久婷婷二区次 | 日韩精品视频在线 | 国产精品污www一区二区三区 | 精品无码久久久久久国产 | 伊人久麻豆社区 | 九九热精品视频在线观看 | 一区二区在线 | 国产97久久 | 国产精品国产a级 | 国精产品一区二区三区 | 成人三级av | 国产线视频精品免费观看视频 | 三区在线| 最新毛片网站 | 久久综合香蕉 | 日本一区二区高清不卡 | 亚洲区在线 | 成人一区二区电影 | 欧美亚洲国产一区 | 91小视频在线| 成人毛片视频在线播放 | a中文在线视频 | 一区二区三区免费观看 | 亚洲av毛片| 日韩av在线中文字幕 | 999国产精品视频免费 | 欧美日韩视频在线播放 | 免费观看一级特黄欧美大片 |