1

このコードをg++ -std=c++11引数付きでコンパイルする際に問題があります:

#include <iostream>
#include <unordered_map>
#include <functional>
#include <iostream>
using namespace std;;

template <typename T> class opt {
    public:
        opt() {};
        ~opt() {};
        virtual int isEmpty() = 0;
        virtual T getObject() = 0;
};

template <typename T> class oopt : public opt<T> {
    private:
        T ret;
    public:
        oopt(T obj) { ret = obj; };
        ~oopt() override;
        int isEmpty() { return 0; };
        T getObject() { return ret; };
};
int main(void) {

    function<opt<int>(int)> fu = [](int j) { return (oopt<int>(10)); };

    return 1;
}

このコードは次のエラーを返します。

assoc_array.cc: In instantiation of ‘class oopt<int>’:
assoc_array.cc:68:63:   required from here
assoc_array.cc:29:3: error: ‘oopt<T>::~oopt() [with T = int]’ marked override, but does not override
In file included from assoc_array.cc:3:0:
/usr/include/c++/4.7/functional: In instantiation of ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = opt<int>; _Functor = main()::<lambda(int)>; _ArgTypes = {int}]’:
/usr/include/c++/4.7/functional:2298:6:   required from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = main()::<lambda(int)>; _Res = opt<int>; _ArgTypes = {int}; typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<opt<int>(int)>::_Useless]’
assoc_array.cc:68:67:   required from here
/usr/include/c++/4.7/functional:1909:7: error: invalid abstract return type for function ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = opt<int>; _Functor = main()::<lambda(int)>; _ArgTypes = {int}]’
assoc_array.cc:7:29: note:   because the following virtual functions are pure within ‘opt<int>’:
assoc_array.cc:11:15: note:     int opt<T>::isEmpty() [with T = int]
assoc_array.cc:12:13: note:     T opt<T>::getObject() [with T = int]
In file included from assoc_array.cc:3:0:
/usr/include/c++/4.7/functional:1912:40: error: cannot allocate an object of abstract type ‘opt<int>’
assoc_array.cc:7:29: note:   since type ‘opt<int>’ has pure virtual functions
In file included from assoc_array.cc:3:0:
/usr/include/c++/4.7/functional:1743:2: error: ‘static _Functor* std::_Function_base::_Base_manager<_Functor>::_M_get_pointer(const std::_Any_data&) [with _Functor = main()::<lambda(int)>]’, declared using local type ‘main()::<lambda(int)>’, is used but never defined [-fpermissive]

インターフェイスへの静的キャストでクロージャーを使用した場合のエラーはどこにありますか? 私はすでに同様の返信を読んだことがあります(抽象クラ​​スでの C++ と継承C++ - 派生クラスでの「宣言されていないメンバー関数」)が、クロージャー定義を削除してもコンパイル時にエラーが発生しないようです。前もって感謝します。

編集

次のコードでも:

#include <iostream>
#include <unordered_map>
#include <functional>
#include <iostream>
using namespace std;;

template <typename T> class opt {
    public:
        opt<T>(T) {};
        ~opt<T>() {};
        virtual int isEmpty() = 0;
        virtual T getObject() = 0;
};

template <typename T> class oopt : public opt<T> {
    private:
        T ret;
    public:
        oopt<T>(T obj) { ret = obj; };
        ~oopt<T>() override;
        int isEmpty() { return 0; };
        T getObject() { return ret; };
};
int main(void) {

    function<opt<int>(int)> fu = [](int j) { return (oopt<int>(10)); };

    return 1;
}

私はまだ次のコンパイルの問題があります。

assoc_array.cc: In instantiation of ‘class oopt<int>’:
assoc_array.cc:26:64:   required from here
assoc_array.cc:20:4: error: ‘oopt<T>::~oopt() [with T = int]’ marked override, but does not override
assoc_array.cc: In instantiation of ‘oopt<T>::oopt(T) [with T = int]’:
assoc_array.cc:26:64:   required from here
assoc_array.cc:19:19: error: no matching function for call to ‘opt<int>::opt()’
assoc_array.cc:19:19: note: candidates are:
assoc_array.cc:9:4: note: opt<T>::opt(T) [with T = int]
assoc_array.cc:9:4: note:   candidate expects 1 argument, 0 provided
assoc_array.cc:7:30: note: constexpr opt<int>::opt(const opt<int>&)
assoc_array.cc:7:30: note:   candidate expects 1 argument, 0 provided
In file included from assoc_array.cc:3:0:
/usr/include/c++/4.7/functional: In instantiation of ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = opt<int>; _Functor = main()::<lambda(int)>; _ArgTypes = {int}]’:
/usr/include/c++/4.7/functional:2298:6:   required from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = main()::<lambda(int)>; _Res = opt<int>; _ArgTypes = {int}; typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<opt<int>(int)>::_Useless]’
assoc_array.cc:26:68:   required from here
/usr/include/c++/4.7/functional:1909:7: error: invalid abstract return type for function ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = opt<int>; _Functor = main()::<lambda(int)>; _ArgTypes = {int}]’
assoc_array.cc:7:30: note:   because the following virtual functions are pure within ‘opt<int>’:
assoc_array.cc:11:16: note:     int opt<T>::isEmpty() [with T = int]
assoc_array.cc:12:14: note:     T opt<T>::getObject() [with T = int]
In file included from assoc_array.cc:3:0:
/usr/include/c++/4.7/functional:1912:40: error: cannot allocate an object of abstract type ‘opt<int>’
assoc_array.cc:7:30: note:   since type ‘opt<int>’ has pure virtual functions
In file included from assoc_array.cc:3:0:
/usr/include/c++/4.7/functional:1743:2: error: ‘static _Functor* std::_Function_base::_Base_manager<_Functor>::_M_get_pointer(const std::_Any_data&) [with _Functor = main()::<lambda(int)>]’, declared using local type ‘main()::<lambda(int)>’, is used but never defined [-fpermissive]
4

3 に答える 3

0

まず、オーバーライドを取り除き、デストラクタを仮想にします (継承されるときは常にそうあるべきです)。また、基本クラスではデストラクタが実装されていません

#include <iostream>
#include <unordered_map>
#include <functional>
#include <iostream>
using namespace std;;

template <typename T> class opt {
    public:
        opt() {};
        virtual ~opt() {};
        virtual int isEmpty() = 0;
        virtual T getObject() = 0;
};

template <typename T> class oopt : public opt<T> {
    private:
        T ret;
    public:
        oopt(T obj) { ret = obj; };
        ~oopt(){};
        int isEmpty() { return 0; };
        T getObject() { return ret; };
};
int main(void) {

    function<opt<int>(int)> fu = [](int j) { return (oopt<int>(10)); };

    return 1;
}

これでコンパイルできます。あなたが元々得ていたエラーに関しては、私はそれらを取得しなかったので、それらがどこから来たのかは言えません

于 2013-06-09T22:58:44.637 に答える