【问题标题】:forward-declaration in c++C++中的前向声明
【发布时间】:2012-11-28 10:47:19
【问题描述】:

我的程序包括两个主要部分。第一个是DLL中的c++类定义,另一个是核心程序。每个DLL加载到核心程序上后,Proxy类会将类描述填充到核心程序中的数据结构中,使用关键字“extern”解决

我遇到了带有错误消息的声明顺序问题。 在代码行中:“typedef map method_map;” 1. 错误:“代理”未在此范围内声明 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_;
}
};    

我希望看到您对此问题的建议。非常感谢!

【问题讨论】:

    标签: c++ dynamic-linking forward-declaration


    【解决方案1】:

    method_mapclass_map_ 需要嵌套在 proxy(或其他模板)中,因为它们依赖于另一个嵌套类型 (method),而后者又取决于模板参数。

    如果它们没有(例如,如果 proxy 是类而不是模板),则需要在 proxy 之后声明它们才能使用在那里声明的类型。

    【讨论】:

    • 非常感谢。我试图在类代理中声明 class_map_,但由于 class_map_ 是用关键字“extern”声明的,因此我收到消息错误,不能将其放入类中。您能否提供一些其他建议。我希望尽快看到您的回复。
    【解决方案2】:

    typedef map&lt;string, proxy::method&gt; method_map;之前定义/声明class proxy;

    像这样:

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

    【讨论】:

    • 这将如何工作?编译器如何知道method 中的proxy 是什么。
    • @Dani 有一个叫做接口的东西。
    • @Aniket:我尝试了您的解决方案如下: t template class proxy; typedef map method_map;但我在代码行“typedef ...”中收到相同的错误消息“模板参数 2 无效”。我认为原因是 proxy::method 需要一个模板参数。您能否提供其他建议。非常感谢!
    • @user1859177 typedef map&lt;string, proxy&lt;your_type&gt;::method&gt; method_map
    • @Aniket:很伤心,我不知道 my_type 是什么。将来可能是任何类型的课程。我尝试了 proxy::method,但出现错误:“'T' is not declared in this scope”
    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多