【问题标题】:typedef variable in C++C++ 中的 typedef 变量
【发布时间】:2012-03-24 04:22:19
【问题描述】:

我想问一下 C++ 中的 typedef 变量

好的,现在我正在使用 PCL,我想将代码分成 .h 和 .cpp

这是我的 .h 文件

template <typename PointType>
class OpenNIViewer
{
public:
    typedef pcl::PointCloud<PointType> Cloud;
    typedef typename Cloud::ConstPtr CloudConstPtr;

    ...
    ...

    CloudConstPtr getLatestCloud ();

    ...
    ...
};

然后在其他.cpp文件上定义getLatestCloud()

template <typename PointType>
CloudConstPtr OpenNIViewer<PointType>::getLatestCloud ()
{
    ...
}

然后我收到 C4430 错误,因为它无法识别返回类型 CloudConstPtr

抱歉这个愚蠢的问题:D

【问题讨论】:

    标签: c++ templates typedef typename


    【解决方案1】:

    CloudConstPtr 是嵌套类型,因此您还需要使用范围对其进行限定:

    template <typename PointType>
    typename OpenNIViewer<PointType>::CloudConstPtr OpenNIViewer<PointType>::getLatestCloud ()
    {
        ...
    }
    

    但它仍然会工作:这是因为你已经在.cpp 文件中定义了它。在模板的情况下,定义应该在.h 文件本身中可用。最简单的方法是在类本身中定义每个成员函数。不要写.cpp 文件。

    【讨论】:

    • 啊,谢谢你的回答,无论如何,我发现模板类不应该被分隔为 .h 和 .cpp 我应该把它们都放在一个 .h 上:D
    • @RezaAdhityaSaputra:是的。将它们全部放在单个 .h 文件中
    【解决方案2】:

    getLatestCloud 更改为:

    template <typename PointType>
    typename OpenNIViewer<PointType>::CloudConstPtr
    OpenNIViewer<PointType>::getLatestCloud ()
    {
        ...
    }
    

    在读取CloudConstPtr时,编译器还不知道应该在哪个作用域中查找,所以需要对其进行限定。

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 2015-03-24
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2015-12-14
      • 2011-04-17
      • 1970-01-01
      相关资源
      最近更新 更多