一次发布多个数据

时间:2017-11-30 20:01:00

标签: c++ function callback

我有一个头文件,其中包含以下格式#define <name_of_error> <word>,<bit> "<name_of_error>

的宏列表

以下是标题文件的一部分 -

#define CAMERA_ERROR 0,0, "CAMERA_ERROR"
#define IMAGE_NOT_DISPLAYING 0,1, "IMAGE_NOT_DISPLAYING"
#define CHANGE_IMAGE_CONSISTENCY 1,2, "CHANGE_IMAGE_CONSISTENCY"

现在我有一个cpp源文件,我在其中创建了一个函数pubError并将头文件信息作为参数传递 -

void pubError(int word, int bit, string name_of_error)
{
  // Body of the function
}

我有一个回调函数,我尝试从其中调用函数pubError(并传递name_of_error),如下所示 -

void sensorCallback(const diagnostic_msgs::DiagnosticArrayPtr &msg)
{
  msg->status[0].level == diagnostic_msgs::DiagnosticStatus::ERROR
  this->sc_.pubError(CAMERA_ERROR);
}

通过这种方式,我只能在特定时间发布1个错误。如何通过函数pubError

从头文件中发布多个错误

1 个答案:

答案 0 :(得分:2)

有多种方法可以解决这个问题。假设您不想更改pubError,则可以按如下方式更改struct Error { int e1; int e2; std::string error; }; void pubError(std::vector<Error> errors) { for (auto e : errors) std::cout << e.error.c_str() << std::endl; } 功能:

#define CAMERA_ERROR 0,0, "CAMERA_ERROR"
#define IMAGE_NOT_DISPLAYING 0,1, "IMAGE_NOT_DISPLAYING"
#define CHANGE_IMAGE_CONSISTENCY 1,2, "CHANGE_IMAGE_CONSISTENCY"

pubError({ { CAMERA_ERROR }, { IMAGE_NOT_DISPLAYING } });

然后你可以这样调用它:

     <!DOCTYPE html>
       <html>
      <head>
      <meta charset='utf-8' />
      <title>Interactive Earthquake Live Map</title>
     <meta name="description" content=" Live real time maps of Earthquakes, 
      Volcanoes, Lightning and Storms across the world" />
    <link 
     href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" 
     rel="stylesheet">
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-
    scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-
     gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-
     gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0%; bottom:0%; left:0%; width:100%; }
       .mapboxgl-popup-content {padding:10px; background:rgba(54, 69, 79, 0.8); 
      color:#fff; pointer-events:none;}
      </style>
   </head>
      <body>
     <div id='map'></div>
      <script>
            mapboxgl.accessToken ='pk.eyJ1IjoiaHlkcmFpZGVzIiwiYSI6ImNpdTFtMWx0MjAwMGIydHA4YnYyM2FteWgifQ.7Ox9rOJrv2p07vREPK2XFw';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v9',
    center: [ 2.35, 48.85 ],
    zoom: 3
});

map.on('load', function() {
    map.addSource("earthquakes", {
        type: "geojson",
        data: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson",
    });
map.addLayer({
        "id": "earthquake-layer",
        "type": "circle",
        "source": "earthquakes",
        "paint": {
            "circle-color": {
             property: 'mag',
                    stops: [
                        [3, '#fed976'],
                        [4, '#fd8d3c'],
                        [5, '#ff0000'],
                        [6, '#830300'],
                        [9, '#b10026'],
]
                },
             "circle-opacity": 0.6,
            "circle-stroke-width": 1,
          "circle-stroke-color": "#000",
            "circle-blur": 0.1,
            "circle-radius":50,
        },
    })
});
</script>
</body>
</html>