adamzhl 2023-03-05 22:53 采纳率: 64.7%
浏览 85
已结题

c++编译问题,编译发生错误

c++编译可以通过,运行的时候崩溃,运行到setCustom函数,this值为NULL,str输入值正常

class GooseStatInfo {
private:
    std::string _custom;

public:
    void setCustom(const std::string& str) {
        _custom = str;
        //debug 编译到此处str为字符串,this 值为NULL
    }

    std::string getToString() {
        Serial gs;
        gs.update(_custom);

        // ... other codes
    }
};
class ABRInt {
private:
    GooseStatInfo* _stat;

    ABRInt(ABRConfig& config, StatInfo* s) {
        _stat = s;

        // ... other codes
    }

    void onEvent(const std::shared_ptr<ABREvent>& event) {
        std::string results = ABRCustC::getStatInfo();
        //需要把results的结果发到_custom,ABRCustC::getStatInfo()返回的结果为str类型,做了c.str()处理
        _stat->setCustom(results);
        
        // ... other codes
    }
};

编译输出

img

编译语法没有错误

  • 写回答

3条回答 默认 最新

  • b2b160 2023-03-05 23:40
    关注
    
    class ABRInt {
    private:
        GooseStatInfo* _stat;
        ABRInt(ABRConfig& config, StatInfo* s) {
            _stat = s;
            // ... other codes
        }
        void onEvent(const std::shared_ptr<ABREvent>& event) {
            std::string results = ABRCustC::getStatInfo();
            //需要把results的结果发到_custom,ABRCustC::getStatInfo()返回的结果为str类型,做了c.str()处理
    //这里加上判断就可以了
    if(_stat != NULL)
            _stat->setCustom(results);
            // ... other codes
        }
    };
    
    
    

    应该是事件发生前还未运行初始化代码

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月13日
  • 已采纳回答 3月5日
  • 赞助了问题酬金15元 3月5日
  • 修改了问题 3月5日
  • 展开全部

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!