简体   繁体   English

调用模板成员 function 和 object 和私有成员

[英]Call to a templated member function with an object with private members

I have this templated function which argument is the member function of any object:我有这个模板 function 哪个参数是任何 object 的成员 function:

// Function that captures the data
template <class T>
bool captureData(T& f_captureObject, bool (T::* f_captureFunction)())
{
    // Lock data access
    std::unique_lock<std::mutex> lock(m_mutex);

    // Capture data and check if it is ready
    if ((f_captureObject.*f_captureFunction)() == true)
    {
        // If data is ready ...

        // Set data flag to ready
        m_isReady = true;

        // Notify if processing thread is waiting
        m_conditionVariable.notify_one();
    }
    else
    {
        // If data is not ready ...

        // Set data flag to not ready
        m_isReady = false;
    }

    // Return grab state
    return !m_isReady;
};

The way I call it is:我叫它的方式是:

m_monitorLidar.captureData<Lux>(m_lidar, &Lux::captureScanOffline);

Where m_lidar is a Lux object whose.hpp is:其中m_lidarLux object,其.hpp 为:

#pragma once

// Includes
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Standard
#include <iostream>
#include <fstream>
#include <thread>

// User
#include "PlatformTypes.hpp"
#include "VariableTypes2.hpp"
#include "Timer.hpp"

// Namespaces
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using namespace std;
using namespace chrono;

// Class
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Lux
{

// Variables
// ------------------------------------------------------------------------------------------------

private:

    // Scan number
    uint32 m_scanNumber;

    // Scan captured
    Scan m_scanCaptured;

    // Scan saved
    Scan m_scanSaved;

    // Flag indicating if scan is captured
    bool m_isCaptured;

    // Flag indicating if scan is saved
    bool m_isSaved;

    // Scans file
    string m_fileScans;

    // Lidar points file stream
    ifstream m_lidarPointsFileStream;

    // Number of scans of a file
    uint32 m_numScansFile;

    // Timer for the simulation
    Timer m_timer;

    // Scan rate for the simulation
    uint64 m_scanRate;

// Functions
// ------------------------------------------------------------------------------------------------

public:
    
    // Constructor
    Lux();

    // Destructor
    ~Lux();

    // Initilization function
    void init();

    // Initilization function in offline mode
    bool initOffline(string f_fileScans);

    // Initilization function in online mode
    bool initOnline();

    // Initilization function in simulation mode
    bool initSimulation(string f_fileScans, uint64 f_scanRate);

    // Capture scan offline
    bool captureScanOffline();

    // Capture scan online
    bool captureScanOnline();

    // Capture scan simulation
    bool captureScanSimulation();

    // Save the captured scan
    void saveScan();

    // Get scan points from sensor/file
    bool getScanPoints();

    // Getters
    uint32 getScanNumber();
    const Scan& getScanCaptured();
    const Scan& getScanSaved();
    bool getIsCaptured();
    bool getIsSaved();

private:

     //Open lidar file
    bool openFile();

    // Close lidar file
    void closeFile();
};

And its.cpp file is:它的.cpp 文件是:

#include "Lux.hpp"

Lux::Lux()
{
    init();
}

Lux::~Lux()
{
    // Close lidar file
    closeFile();
}

void Lux::init()
{
    // Error flag
    bool error = false;

    // Scan number
    m_scanNumber = 0U;

    // Init stereo images
    m_scanCaptured.init();

    // Init flag indicating if scan is captured
    m_isCaptured = false;

    // Set flag indicating scan is not saved
    m_isSaved = false;
}

bool Lux::initOffline(string f_fileScans)
{
    // Function error
    bool error = false;

    // Set file path
    m_fileScans = f_fileScans;

    // Open file
    error = openFile();

    // Return error status
    return error;
}

bool Lux::initOnline()
{
    // Function error
    bool error = false;

    // Return error status
    return error;
}

bool Lux::initSimulation(string f_fileScans, uint64 f_scanRate)
{
    // Function error
    bool error = false;

    // Init in offline mode
    error = initOffline(f_fileScans);

    // Set scan rate
    m_scanRate = f_scanRate;

    // Return error status
    return error;
}

bool Lux::captureScanOffline()
{
    // Check if there are scans
    if (m_scanNumber < m_numScansFile)
    {
        // If there are scans ...

        // Read number of echoes
        m_lidarPointsFileStream.read((char*)(m_scanCaptured.ne), p_maxNumberOfPointsPerEcho * sizeof(uint32));

        // Read radial distance
        m_lidarPointsFileStream.read((char*)(m_scanCaptured.sc.rd), p_maxNumberOfPointsPerScan * sizeof(float32));

        // Read x-coordinate
        m_lidarPointsFileStream.read((char*)(m_scanCaptured.cc.x), p_maxNumberOfPointsPerScan * sizeof(float32));

        // Read y-coordinate
        m_lidarPointsFileStream.read((char*)(m_scanCaptured.cc.y), p_maxNumberOfPointsPerScan * sizeof(float32));

        // Read z-coordinate
        m_lidarPointsFileStream.read((char*)(m_scanCaptured.cc.z), p_maxNumberOfPointsPerScan * sizeof(float32));

        // Set is captured flag to true
        m_isCaptured = true;
    }
    else
    {
        // If there are no scans ...

        // Print message
        cout << "No more scans" << endl;

        // Set is captured flag to false
        m_isCaptured = true;
    }

    // Return if there was an error
    return !m_isCaptured;

    return false;
}

bool Lux::captureScanOnline()
{
    return false;
}

bool Lux::captureScanSimulation()
{
    // Function error
    bool error = false;
    // Waiting time to achive the scan rate
    int64 waitTime = 0LL;

    // Start timer
    m_timer.start();

    // Capture scan offline
    error = captureScanOffline();

    // Stop timer
    m_timer.stop();

    // Measure the waiting time
    waitTime = m_scanRate - m_timer.elapsed();

    // If capture was to fast
    if (waitTime > 0LL)
    {
        // Wait a time to achive the scan rate
        this_thread::sleep_for(milliseconds(waitTime));
    }

    // Return error status
    return error;
}

void Lux::saveScan()
{
    // Copy the stereo images
    m_scanSaved = m_scanCaptured;

    // Set flag indicating stereo is saved
    m_isSaved = true;
}

uint32 Lux::getScanNumber()
{
    return m_scanNumber;
}

const Scan& Lux::getScanCaptured()
{
    return m_scanCaptured;
}

const Scan& Lux::getScanSaved()
{
    return m_scanSaved;
}

bool Lux::getIsCaptured()
{
    return m_isCaptured;
}

bool Lux::getIsSaved()
{
    return m_isSaved;
}

// Private functions definition
// --------------------------------------------------------------------------------------------------------------------

bool Lux::openFile()
{
    // Open file
    m_lidarPointsFileStream.open(m_fileScans, ifstream::binary);
    if (!m_lidarPointsFileStream.is_open())
    {
        cout << "Lidar points file could not be opened." << endl;
        return true;
    }

    // Read the number of scans
    m_lidarPointsFileStream.read((char*)(&m_numScansFile), sizeof(uint32));

    return false;
}

void Lux::closeFile()
{
    // Close lidar file
    m_lidarPointsFileStream.close();
}

bool Lux::getScanPoints()
{

}

The question is, what the Lux object has to get this error.问题是,Lux object 必须得到这个错误。 With other objects I haven't this error.对于其他对象,我没有这个错误。

C++ function "Lux::Lux(const Lux &)" (declared implicitly) cannot be referenced -- it is a deleted function C++ function "Lux::Lux(const Lux &)"(隐式声明)不能被引用——它是一个已删除的 function

Thanks in advance.提前致谢。

I found the solution using std::ref() when passing the object:我在传递 object 时使用 std::ref() 找到了解决方案:

m_monitorLidar.captureData<Lux>(std::ref(m_lidar), &Lux::captureScanOffline);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用模板化成员函数与模板化全局函数以在通用工厂中创建对象 - Call templated member function vs. templated global function to create object in generic factory 访问作为结构成员的对象的模板化成员函数 - accessing templated member function of an object that is a member of a struct Call to a templated member function that takes as arguments an object and one of its member function in C++ - Call to a templated member function that takes as arguments an object and one of its member function in C++ 封装成员函数调用的模板化类的行为 - Behaviour of templated class wrapping a member function call 用std :: async调用模板成员函数 - Call a templated member function with std::async 从受限制的(静态)成员函数访问受保护的成员 - Access protected members from templated (static) member function 将模板结构的对象传递给另一个模板类的成员函数 - Passing object of a Templated struct to a member function of another templated class 模板化的成员函数调用g ++错误:没有匹配的调用函数 - Templated member function call g++ error : no matching function for call 模板类的模板化隶属函数 - Templated Member Function of Templated Class 访问私人成员的私人成员 - Accessing private members of a private member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM