簡體   English   中英

C++ 沒有可行的構造函數復制類型的變量

[英]C++ no viable constructor copying variable of type

我收到以下錯誤消息:

main.cpp:沒有可行的構造函數復制“公共”類型的變量

在第二個構造函數communal( const T& instance ) ,給出了以下消息:

data.h:候選構造函數不可行:第一個參數沒有從“公共”到“const int &”的已知轉換

轉換似乎正在倒退。 我希望轉換從 const int& 到公共。 有沒有辦法讓隱式轉換在這里工作? 謝謝你的幫助。

主.cpp:

communal<int> test_communal1 = 123; // Implicit initialization triggers error

數據.cpp:

template<typename T>
struct communal : general_type::communal<T>
{
    using super = general_type::communal<T>;

    communal() : super( nullptr ) {}
    communal( const T& instance ) : super( new T( instance ) ) {}
    communal( const T* instance ) : super( new T( instance ) ) {}
    communal( communal<T>& instance ) : super( instance ) {}
    communal( communal<T>* instance ) : super( instance ) {}

    ~communal()
    {
        this->counter->deallocate( [this]()
        {
            delete this->counter;
            delete this->instance;
        });
    }
};

我想出了解決這個問題的辦法。 我的目的是讓以下轉換構造函數工作communal( const T& instance ) 我犯了一個錯誤,為我的復制構造函數保留了 const。 出於某種原因,推斷使用錯誤的復制構造函數以及從communal<int>const int&潛在轉換。 將 const 添加到構造函數中,它解決了這個問題。 盡管如此,它似乎確實是一個錯誤,它不會優先使用轉換構造函數。 也許通過更多地研究這個問題,我可以更深入地理解為什么它優先於其他構造函數而不是轉換構造函數。 不過,這將不得不等待現在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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