c++,构造函数类初始化

在构造函数中对 _stat 进行初始化,但是在调用 _stat->setCustom(results) 函数时,_stat一直未NULL


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(): _As(...), _bs(...) {}
    ABRInt(ABRConfig& config, GooseStatInfo* 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);
        //_stat初始化有点不对,这里_stat一直未NULL
        
        // ... other codes
    }
};
 


要看你在哪里构造ABRInt,是否传入了StateInfo对象,然后看在哪个位置产生了Event,不然不好判断问题出在哪里。

首先,你需要检查下在哪里调用了ABRInt的构造方法,在构造方法中传入的StatInfo* s中的s为NULL了,才会导致_stat 来NULL。你可以在ABRInt的构造方法中把_stat 的值输出来看看:

 ABRInt(ABRConfig& config, StatInfo* s) {
        _stat = s;
 
         在这里把_stat 打印出来看看是否为空,或者debug到此处看_stat 是否为空
    }

其次,检查有没有在代码中将_stat 的值设置为了NULL的地方。
望采纳哦哦

你有两个构造函数

“Devil组”引证GPT后的撰写:
在 ABRInt 的构造函数中添加以下代码:

ABRInt(ABRConfig& config, StatInfo* s) : _stat(s) {
  if (!_stat) {
    _stat = new GooseStatInfo();
  }
  // other codes
}

将 s 赋值给 _stat 变量,如果 s 为 NULL,则创建一个新的 GooseStatInfo 对象并将其赋值给 _stat。这样做可以确保 _stat 变量被正确初始化。

参考GPT和自己的思路,从您提供的代码来看,问题可能出在构造函数的参数 StatInfo* s 上。在构造函数中对 _stat 进行了初始化,但是传入的参数 s 与 _stat 的类型不匹配,因此 _stat 没有被正确初始化,仍然为 NULL。

您可以尝试修改构造函数的参数类型为 GooseStatInfo* s,并在构造函数中对 _stat 进行初始化,例如:

ABRInt(ABRConfig& config, GooseStatInfo* s) {
    _stat = s;
 
    // ... other codes
}

如果仍然遇到问题,您可以在调用构造函数时检查参数 s 是否为 NULL。同时,还需要确保在调用 onEvent 函数之前 _stat 已经被正确初始化。

参考GPT的回答内容,根据你提供的代码,构造函数中传递给 _stat 的指针类型不确定,可能会导致 _stat 为 NULL 的情况。需要对构造函数进行修改来解决这个问题。

另外,StatInfo 类型未定义,建议将 GooseStatInfo* 类型改为 StatInfo* 类型或者将 StatInfo 定义为 GooseStatInfo 类型。

修改后的代码如下所示:

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:
    StatInfo* _stat; // 将 GooseStatInfo* 类型改为 StatInfo* 类型
    ABRInt(): _As(...), _bs(...) {}
    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 != nullptr) { // 添加判断语句,避免出现空指针的情况
            _stat->setCustom(results);
        }
        
        // ... other codes
    }
};


需要注意的是,在调用 _stat->setCustom(results) 之前,需要确保 _stat 不为空指针,否则会出现访问空指针的错误。因此,在调用该函数之前,需要添加一个判断 _stat 是否为空指针的语句。

回答不易,还请采纳!!

在ABRInt的构造函数中,应该将参数s赋值给成员变量_stat,以便调用其成员函数setCustom。另外,需要检查构造函数的实现是否正确,并且确保传递给ABRInt构造函数的指针不为NULL。

在构造函数中对 _stat 进行初始化时,确保已经为其分配了内存空间,或者将其指针初始化为 nullptr。例如:

ABRInt(ABRConfig& config, GooseStatInfo* s) : _stat(s) {
    // ... other codes
}

在调用 _stat->setCustom(results) 时,确保 _stat 不为 nullptr,也就是在调用该函数之前,需要先为 _stat 分配内存空间或者给其赋值。可以添加一些判断语句,例如:

void onEvent(const std::shared_ptr<ABREvent>& event) {
    if (!_stat) {
        std::cout << "Error: _stat is null" << std::endl;
        return;
    }

    std::string results = ABRCustC::getStatInfo();
    _stat->setCustom(results);

    // ... other codes
}

这样可以避免在 _stat 为空指针时导致的访问非法内存的错误。