【问题标题】:error wihle declaring a variable using template使用模板声明变量时出错
【发布时间】:2016-08-27 07:04:57
【问题描述】:

为了使调试过程更容易,我决定声明一个全局变量,它是一个指向类对象的指针,并使用全局指针来访问任何低级类变量。该类是使用模板定义的。在构造类对象的某个时刻,我可以分配全局指针并在其他地方使用它。
就我而言,我尝试添加一个变量类型 Net(这是用于 py-faster-rcnn 代码)。

    namespace caffe {
    ...
        int glb_layer_id;
        template <typename Dtype>
        shared_ptr<Net<Dtype>> glb_Net;

        template <typename Dtype>
        void Net<Dtype>::Init(const NetParameter& in_param) {
            ...
        }

添加int glb_layer_id 没问题,我可以在其他地方使用该变量。但是当我添加一个变量glb_Net时,下面出现了编译错误。

ckim@stph45:~/Neuro/py-faster-rcnn.org] !make make -C caffe-fast-rcnn/ make: 进入目录/home/ckim/Neuro/py-faster-rcnn.org/caffe-fast-rcnn' CXX src/caffe/net.cpp src/caffe/net.cpp:41: error: template declaration of 'caffe::Net<Dtype>* caffe::glb_Net' src/caffe/net.cpp: In member function 'void caffe::Net<Dtype>::Init(const caffe::NetParameter&)': src/caffe/net.cpp:75: error: 'glb_Net' was not declared in this scope make: *** [.build_debug/src/caffe/net.o] Error 1 make: Leaving directory/home/ckim/Neuro/py-faster-rcnn.org/caffe-fast-rcnn'

Net 类最初是使用模板定义的,因此我必须对 Net 变量使用相同的模板。但是可以看到,有一个错误。问题是什么?任何帮助表示赞赏。 (顺便说一下,在 Caffe 中,我也想知道如何从 Net 内部的层访问 Net 变量。

【问题讨论】:

    标签: c++ caffe pycaffe


    【解决方案1】:

    声明一个 shared_ptr 变量是在不指定“模板”关键字的情况下完成的。 而不是声明

    template <typename Dtype> shared_ptr<Net<Dtype>> glb_Net;
    

    您应该将 glb_Net 声明为:

    #include <Dtype.h>
    #include <Net.h>
    shared_ptr<Net<Dtype>> glb_Net;
    

    【讨论】:

    • 嗨,我试过了,使用 shared_prt&lt;Net&lt;Dtype&gt;&gt; glb_Net; 我得到了这样的错误:src/caffe/net.cpp:40: error: 'Dtype' was not declared in this scope src/caffe/net.cpp:40: warning: '&gt;&gt;' operator will be treated as two right angle brackets in C++0x src/caffe/net.cpp:40: warning: suggest parentheses around '&gt;&gt;' expression src/caffe/net.cpp:40: error: 'glb_Net' was not declared in this scope src/caffe/net.cpp:40: error: template argument 1 is invalid src/caffe/net.cpp:40: error: template argument 1 is invalid 有什么想法吗?
    • 我在我的答案中添加了正确的包含。也许您应该包含一个小型示例应用程序,以便我们可以纠正所有问题。您上面的示例只是一个 sn-p,因此我们无法说出其中缺少的所有内容。
    • 在命名空间 caffe 中,我看到 typedef float Dtype; 并包含了 net.hpp。这是复杂代码的一部分,所以我最好做一个简单的例子。我正在修复一个错误,所以我会稍后再做。谢谢。
    • 对于 dbg1.hpp 文件:#include "caffe/net.hpp" extern int glb_layer_id;模板 extern shared_ptr > glb_Net_ptr; ./include/caffe/dbg1.hpp:5: 错误: ' 之前需要 ',' 或 '...'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多