error: passing xxx as 'this' argument of xxx discards qualifiers的解决办法
来源:互联网 发布:oracle 数据库别名 编辑:程序博客网 时间:2024/05/20 18:48
问题:
在controller.cpp的头文件controller.h中,有定义:
class Controller {public: explicit Controller(const string uart_port); ~Controller(); const SDCard& GetSDCard() const; const UartUsb& GetUart() const;private: SDCard sdcard_; UartUsb uart_;};其中类UartUsb的声明为:
class UartUsb {public: UartUsb(); explicit UartUsb(const string uart_port); ~UartUsb(); int OpenPort(const char *com_port); int SetComConfig(int baud_rate, int data_bits, char parity, int stop_bits); void ReadUart(char* buf_rd); void WriteUart(char* buf_wr);private: string uart_port_; int fd_uart_;};
在sensor_reader.cpp中用到了GetUart()函数:
void* SensorReader::Run() { main_ctrlr = static_cast<Controller*>(GetArg()); time_t upload_time = 0; bool en_upload_sdcard_status = false; while (!IsStop()) { // Upload sdcard status only once if (en_upload_sdcard_status) { en_upload_sdcard_status = false; } // Enable uploading sdcard status time(&now_time); if ((now_time - upload_time) >= main_ctrlr->GetUploadInterval()) { upload_time = now_time; en_upload_sdcard_status = true; } // Store sdcard status into data_.buf main_ctrlr->GetUart().ReadUart(main_ctrlr->GetDataBuffer().buf); trim(main_ctrlr->GetDataBuffer().buf); } return NULL;}
编译时出现如下错误提示:
hxtcdt@hxtc-desktop:~/hwsvn/2fw/01prj_mms/s3c6410/src/trunk$ make clean;makearm-linux-g++ -g3 -Wall -O2 -c controller.cpp -o controller.oarm-linux-g++ -g3 -Wall -O2 -c main.cpp -o main.oarm-linux-g++ -g3 -Wall -O2 -c sdcard.cpp -o sdcard.oarm-linux-g++ -g3 -Wall -O2 -c sensor_reader.cpp -o sensor_reader.osensor_reader.cpp: In member function 'virtual void* SensorReader::Run()':sensor_reader.cpp:83: error: passing 'const UartUsb' as 'this' argument of 'void UartUsb::ReadUart(char*)' discards qualifiersmake: *** [sensor_reader.o] 错误 1hxtcdt@hxtc-desktop:~/hwsvn/2fw/01prj_mms/s3c6410/src/trunk$
解决办法:
1。GetUart()返回的是const UartUsb对象;
因此当const UartUsb对象调用ReadUart()时,编译器检测到了问题。
也就是说,对一个const对象调用non-const成员函数是不允许的,因为non-const成员函数不保证一定不修改对象。
因此编译器做出一个安全的假定,假定ReadUart()会试图修改UartUsb对象,而与此同时,该UartUsb对象是const对象,所有试图修改const对象的都会报error。
2。 解决办法是将ReadUart()函数改为const函数:
void ReadUart(char* buf_rd) const;
修改之后,问题解决。
2 0
- error: passing xxx as 'this' argument of xxx discards qualifiers的解决办法
- stl set使用时,passing ‘const xxx’ as ‘this’ argument of 'xxx 'discards qualifiers 丢弃限定符的问题
- (c++) Passing `const' as `this' argument of `' discards qualifiers
- C++ (error: passing ‘const …'’ as ‘this’ argument of ‘…’ discards qualifiers [duplicate])
- 关于passing ‘const *’ as ‘this’ argument of ‘*’ discards qualifiers的问题
- QT passing const as this argument discards qualifiers
- discards qualifiers error
- error C2275: ‘XXX’ : illegal use of this type as an expression 报错的解决方法!
- pointer targets in passing argument n of xxx differ in signedness
- C++编程常见问题—error: passing 'const std::map<>]' discards qualifiers或pass-by-reference-to-const-map导致的“d
- error C2275: 'xxx' : illegal use of this type as an expression
- error C2275: 'xxx' : illegal use of this type as an expression
- error C2275: "xxx"illegal use of this type as an expression
- error C2275: 'xxx' : illegal use of this type as an expression
- error: redefinition of ‘xxx’问题的解决
- Php Fatal error: Allowed memory size of xxx bytes exhausted 的解决办法
- error forward declaration of xxx
- error redefinition of 'xxx' 问题解决
- ios之手势
- 设计模式——单例模式
- Public Yum Server
- 开发app软件杀进美国前三甲的创业故事
- VS2008检测内存泄漏的方法
- error: passing xxx as 'this' argument of xxx discards qualifiers的解决办法
- Android自定义View的实现方法,带你一步步深入了解View(四)
- Unable to instantiate application java.lang.ClassNotFoundException
- Android USER 版本与ENG 版本的差异--MTK官方解释
- 嵌入式Linux中程序自启动问题
- linux 查看版本号
- "Installing Software" has encountered a problem---pydev on ubuntu
- 注册表相关
- JAVA动态代理