#include<iostream>
using namespace std;
class A{
public:
virtual void display(){ cout<<"A"<<endl; }
};
class B : public A{
public:
void display(){ cout<<"B"<<endl; }
};
int main(int argc,char* argv[])
{
A *p = new B();
p->display();
delete p;
return 0;
}
*当基类A的函数display有virtual
时会实现多态执行B中的display
*否则p会被看作A的对象从而执行A中的display
若父类没有virtual声明,子类函数可以和父类函数返回值不同,但函数名和参数列表必须相同.
则子类实例通过”.”调用的是子类的函数
若子类想调用父类的函数,则需要使用 子类实例.父类::函数名() 的形式.
catkin_make
编译并输出编译信息文件
catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=Yes
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
令vscode,读取编译信息文件
c_cpp_properties.json如下
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}
其中
```json
"group": {"kind":"build","isDefault":true},
代表将我们定义的这个task添加到build组里面,这样就可以中Ctrl+Shift+B快捷键来找到编译命令,命令名称就是在label里面定义的,如果”isDefault”:true那么就代表直接执行command,如果为false还需要在build下拉里面选一下,我们这里就是label名字:catkin_make
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在调试配置下拉列表中显示
"type": "cppdbg", // 调试器类型 该值自动生成
"request": "launch", // 调试方式,还可以选择attach
"program": "${workspaceRoot}/devel/lib/waypoint_follower/pure_persuit", //要调试的程序(完整路径,支持相对路径)
"args": [], // 传递给上面程序的参数,没有参数留空即可
"stopAtEntry": false, // 是否停在程序入口点(停在main函数开始)
"cwd": "${workspaceRoot}", // 调试程序时的工作目录
"environment": [], //针对调试的程序,要添加到环境中的环境变量. 例如: [ { "name": "squid", "value": "clam" } ]
"externalConsole": false, //如果设置为true,则为应用程序启动外部控制台。 如果为false,则不会启动控制台,并使用VS Code的内置调试控制台。
"MIMode": "gdb", // VSCode要使用的调试工具
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
“program”参数为要测试节点对应的可执行文件
需要额外终端运行roscore,来运行ROS MASTER节点
“request”参数 launch为启动一个node执行代码,同时可以在vscode中断点调试. attach是执行监听任务
可以在调试窗口中使用GDB命令
1.就叫ROS安装就好
2.直接右键文件夹可creat catkin package
3.(Ctrl+Shift+P) 中可使用ros::showMasterStatus
https://blog.csdn.net/ac_dao_di/article/details/71908444
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
int main( int argc, char** argv )
{
ros::init(argc, argv, "basic_shapes");
ros::NodeHandle n;
ros::Rate r(1);
ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);
// Set our initial shape type to be a cube
uint32_t shape = visualization_msgs::Marker::CUBE;
while (ros::ok())
{
visualization_msgs::Marker marker;
// Set the frame ID and timestamp. See the TF tutorials for information on these.
marker.header.frame_id = "/my_frame";
marker.header.stamp = ros::Time::now();
// Set the namespace and id for this marker. This serves to create a unique ID
// Any marker sent with the same namespace and id will overwrite the old one
marker.ns = "basic_shapes";//命名空间
marker.id = 0;
// Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
marker.type = shape;
// Set the marker action. Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)
marker.action = visualization_msgs::Marker::ADD;//ADD意思是创建或修改
// Set the pose of the marker. This is a full 6DOF pose relative to the frame/time specified in the header
// 1.visualization_msgs::Marker::ADD; 创建或修改
// 2.visualization_msgs::Marker::DELETE;删除
// 3.visualization_msgs::Marker::DELETEALL;删除所有
marker.pose.position.x = 0;
marker.pose.position.y = 0;
marker.pose.position.z = 0;
marker.pose.orientation.x = 0.0;
marker.pose.orientation.y = 0.0;
marker.pose.orientation.z = 0.0;
marker.pose.orientation.w = 1.0;
// Set the scale of the marker -- 1x1x1 here means 1m on a side
marker.scale.x = 1.0;
marker.scale.y = 1.0;
marker.scale.z = 1.0;
// Set the color -- be sure to set alpha to something non-zero!
marker.color.r = 0.0f;
marker.color.g = 1.0f;
marker.color.b = 0.0f;
marker.color.a = 1.0;
marker.lifetime = ros::Duration();
// Publish the marker
while (marker_pub.getNumSubscribers() < 1)
{
if (!ros::ok())
{
return 0;
}
ROS_WARN_ONCE("Please create a subscriber to the marker");
sleep(1);
}
marker_pub.publish(marker);
// Cycle between different shapes
switch (shape)
{
case visualization_msgs::Marker::CUBE:
shape = visualization_msgs::Marker::SPHERE;
break;
case visualization_msgs::Marker::SPHERE:
shape = visualization_msgs::Marker::ARROW;
break;
case visualization_msgs::Marker::ARROW:
shape = visualization_msgs::Marker::CYLINDER;
break;
case visualization_msgs::Marker::CYLINDER:
shape = visualization_msgs::Marker::CUBE;
break;
}
r.sleep();
}
}
####构建名为talker的可执行文件,它由src/talker.cpp源文件构成
add_executable(basic_shapes src / basic_shapes.cpp)
##指定链接库位置将talker与变量${catkin_LIBRARIES}的内容链接
target_link_libraries(basic_shapes $ {catkin_LIBRARIES})
设置Global Options中的Fixed Frame
为发送信息头中的frame_id为my_frame
```cpp #include <ros/ros.h> #include <visualization_msgs/Marker.h>
#include
int main( int argc, char** argv )
{
ros::init(argc, argv, “points_and_lines”);
ros::NodeHandle n;
ros::Publisher marker_pub = n.advertise
ros::Rate r(30);
float f = 0.0; while (ros::ok()) {
visualization_msgs::Marker points, line_strip, line_list;
points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/my_frame";
points.header.stamp = line_strip.header.stamp = line_list.header.stamp = ros::Time::now();
points.ns = line_strip.ns = line_list.ns = "points_and_lines";
points.action = line_strip.action = line_list.action = visualization_msgs::Marker::ADD;
points.pose.orientation.w = line_strip.pose.orientation.w = line_list.pose.orientation.w = 1.0;