简体   繁体   中英

typedef variable in C++

i want to ask about typedef variable in C++

ok, right now i'm using PCL and i want to separate the code into .h and .cpp

here's my .h file

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

    ...
    ...

    CloudConstPtr getLatestCloud ();

    ...
    ...
};

then the definition of getLatestCloud() on other .cpp file

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

then i got C4430 error because it doesn't recognize return type CloudConstPtr

sorry for the silly question :D

CloudConstPtr is a nested type, so you need qualify it with the scope also:

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

But then it still would not work: it is because you've defined it in .cpp file. In case of template, the definition should be available in .h file itself. The simpliest way to do that is to define each member function in the class itself. Don't write .cpp file.

Change getLatestCloud to:

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

When reading CloudConstPtr , the compiler does not yet know which scope it should be looking in, so it needs to be qualified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM