C + + External function modifies the value of a class member by pointer

Source: Internet
Author: User

1. Problem arises

I turn my C code into C + +. In my code, a class that creates a new location data, I originally parsed a GPS string with C code and then assigned it a value. The code snippet is as follows

// Extracting NMEA-0183 Information // GPSX:NMEA Information Structure // BUF: The first address of the received GPS data buffer void Gps_analysis (nmea_msg *gpsx,uint8_t *buf) {    if((strstr (constChar *) BUF,"$GPRMC")))        nmea_gprmc_analysis (gpsx,buf);     // GPRMC parsing }

Originally passed in is a global structure variable, this code is not a problem.

But now I'm passing in a variable like this: a struct pointer in a class passes through the function.

class pack{    public:         nmea_msg msg;}; Pack pack; Gps_analysis (&pack.msg,temp_buf);

But I found a problem, I parse the data, but this C function can not be assigned to my structure by pointers, which is very painful.

2. Problem solving

I'll try it. GPS.C in the Gps.cpp, and then change this parameter to the way the reference is passed.

void Gps_analysis (nmea_msg & gpsx,uint8_t *buf) {        nmea_gprmc_analysis (&gpsx,buf);    // GPRMC parsing }

But! I TM found every successful assignment, the second time will be reported segmentation fault. This makes me more sore.

Finally, I found that this is because of the value passed in, sometimes strstr cannot find the location of the parameter, the return value is null, but it also operates on NULL, this situation occurs.

C + + External function modifies the value of a class member by pointer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.