警告"warning: type qualifiers ignored on function return type"

本文探讨了在C/C++中使用const限定符作为函数返回值的问题,解释了为何编译器会忽略const限定符,并提供了正确的使用示例。

我用的时候,使用的是:

定义: const uint16_t getfun(void){int a = 1; return a };
使用: uint16_t num = getfun();
这时,会告诉你,类型限定符const,在函数返回时被忽视了。   因为你返回的是一个局部变量的值. 没有使用引用,也没有使用指针,你使用const其实没有意义的。所以编译器自动给你忽略了。

如果你把Num定义成变量,然后返回它的引用,加const,就有效果,不会报错了。就是这样的。试验也成功了。


警告: 类型限定符在函数返回类型时被忽视, 如果返回const类型, 即会出现这种警告,删除const, 即可;

如: 

[cpp]  view plain  copy
 print ?
  1. //const double _processPornVideo(void); //警告  
  2. double _processPornVideo(void); 

/home/robot/sensor_sync/src/gscam3/src/gscam.cpp:70:13: error: ‘bool gscam::GSCam::reopen_on_eof_’ is private within this context 70 | if(gscam->reopen_on_eof_) { | ^~~~~~~~~~~~~~ In file included from /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:25: /home/robot/sensor_sync/src/gscam3/include/gscam/gscam.h:45:8: note: declared private here 45 | bool reopen_on_eof_; | ^~~~~~~~~~~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp: In function ‘GstFlowReturn gscam::gst_new_preroll_cb(GstAppSink*, gpointer)’: /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:90:36: error: ‘GstElement* gscam::GSCam::pipeline_’ is private within this context 90 | if (gst_element_set_state(gscam->pipeline_, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) { | ^~~~~~~~~ In file included from /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:25: /home/robot/sensor_sync/src/gscam3/include/gscam/gscam.h:52:16: note: declared private here 52 | GstElement * pipeline_; | ^~~~~~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp: In member function ‘bool gscam::GSCam::configure()’: /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:158:56: error: ‘YUV422_YUY2’ is not a member of ‘sensor_msgs::image_encodings’ 158 | image_encoding_ != sensor_msgs::image_encodings::YUV422_YUY2 && | ^~~~~~~~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp: In member function ‘bool gscam::GSCam::init_stream()’: /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:227:63: error: ‘YUV422_YUY2’ is not a member of ‘sensor_msgs::image_encodings’ 227 | } else if (image_encoding_ == sensor_msgs::image_encodings::YUV422_YUY2) { | ^~~~~~~~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp: In member function ‘void gscam::GSCam::publish_stream(GstBuffer*)’: /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:376:63: error: ‘YUV422_YUY2’ is not a member of ‘sensor_msgs::image_encodings’ 376 | } else if (image_encoding_ == sensor_msgs::image_encodings::YUV422_YUY2) { | ^~~~~~~~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp: At global scope: /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:398:1: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] 398 | const unsigned int GSCam::bytes_per_pixel(const std::string & encoding) | ^~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp: In member function ‘const unsigned int gscam::GSCam::bytes_per_pixel(const string&)’: /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:403:56: error: ‘YUV422_YUY2’ is not a member of ‘sensor_msgs::image_encodings’ 403 | } else if (encoding == sensor_msgs::image_encodings::YUV422_YUY2) { | ^~~~~~~~~~~ /home/robot/sensor_sync/src/gscam3/src/gscam.cpp:411:1: warning: control reaches end of non-void function [-Wreturn-type] 411 | } | ^ make[2]: *** [CMakeFiles/gscam3.dir/build.make:63:CMakeFiles/gscam3.dir/src/gscam.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:821:CMakeFiles/gscam3.dir/all] 错误 2 make: *** [Makefile:141:all] 错误 2 <== Failed to process package 'gscam3': Command '['make', '-j16', '-l16']' returned non-zero exit status 2. Reproduce this error by running: ==> cd /home/robot/sensor_sync/build_isolated/gscam3 && make -j16 -l16 Command failed, exiting.
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值