簡體   English   中英

C ++中的前向聲明

[英]forward-declaration in c++

我的程序包括兩個主要部分。 第一個是DLL中的c ++類定義,另一個是核心程序。 在將每個DLL加載到核心程序之后,Proxy類會將類描述填充到核心程序的數據結構中,這可以通過使用關鍵字“ extern”來解決。

我收到了錯誤消息的聲明順序問題。 在代碼行中:“ typedef map method_map;” 1.錯誤:此范圍內未聲明'proxy'。2.錯誤:代碼行中的模板參數2無效:

typedef common_object *maker_t();
extern map< string, maker_t* > factory;


//This method_map is used to store all the method structure data of each class
//method: class_name, method_name, function pointer
//I got two errors here:
//1. "ERROR: ‘proxy’ was not declared in this scope"
//2. "ERROR: ‘error: template argument 2 is invalid"
typedef map<string, proxy::method> method_map;
//this class_map contain the methods description for each class.
//this class_map is declared in the core program.
//after the class in this dll is loaded on the core program,
//it would automatically fill its descriptino in here
extern  map<string, method_map> class_map_;

// our global factory
template<typename T>
class proxy {
public:
typedef int (T::*mfp)(lua_State *L);
typedef struct {
    const char *class_name;
    const char *method_name;
    mfp mfunc;
} method;

proxy() {
    std::cout << "circle proxy" << endl;
    // the loop for filling the methods information of the class T
    method_map method_map_;
    for (method *m = T::methods;m->method_name; m++) {
        method m1; //specific information about each method
        m1.class_name = T::className;
        m1.method_name = m->method_name;
        m1.mfunc = m->mfunc;
        method_map_[m1.method_name] = m1; //assign m1 into the method map
    }
    //Assign methods description of the T class into the class_map
    class_map_[T::class_name] = method_map_;
}
};    

我希望看到您對這個問題的建議。 非常感謝!

method_mapclass_map_將需要嵌套在proxy (或其他模板)中,因為它們依賴於另一個嵌套類型( method ),后者又取決於template參數。

如果不是(例如, proxy是類而不是模板),則需要使用proxy之后聲明它們,以便使用在那里聲明的類型。

轉發定義/聲明class proxy; typedef map<string, proxy::method> method_map;

像這樣:

    template<class T>
    class proxy;
    typedef map<string, proxy::method> method_map;

暫無
暫無

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

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