0
 > -Severity (Error)
 > -ID(local): 1
 > -Code: UNINIT.CTOR.MUST --> IPCAtomic()
 > -Message: 'this->value' is not initialized in this constructor.
 > -Details:
 > 
 > 'this->value' is not initialized in this constructor.
 > 
 > * CommunicationTypes.h:198: 'this->value' is used, but is
 > uninitialized.
 > 
 > * CommunicationTypes.h:198: List of instantiations (may be
 > incomplete)
 > 
 > * CommunicationTypes.h:198: <anonymous>::IPCAtomic< ,
 > >::#constructor
 > 
 > Current status 'Analyze'

这是我的代码,我也尝试了其他选项,但 KW 仍然产生相同的错误

     template <typename T, typename SerializeAs = T>
class IPCAtomic : public IPCBundleIfc
{
    typedef IPCAtomic<T, SerializeAs> MyType;
public:
    T value;

    IPCAtomic() : value(T())
    {
    }

    IPCAtomic(T v) : value(v)
    {
        static_assert(!std::is_base_of<IPCBundleIfc, T>::value, "type parameter of this class can not derive from IPCBundleIfc");
    }

    virtual ~IPCAtomic() {}

    operator T& ()
    {
        return value;
    }

    MyType& operator=(const T& v)
    {
        if (&v != &value)
        {
            value = v;
        }

        return *this;
    }

    bool operator==(const T& v) const
    {
        return value == v;
    }

    bool operator==(const MyType& v) const
    {
        return value == v.value;
    }

你能提供任何解决方案吗?

4

1 回答 1

1

要初始化模板化成员值,请使用列表初始化而不是从默认构造对象初始化。

像这样 :

IPCAtomic() : 
    value{}
{
}

// when initializing with a value use const T& this avoids 
// unecessary copies. Also make constructors with one parameter
// explicit so they can't accidentaly be used as type conversions.
explicit IPCAtomic(const T& v) : 
    value{v}
{
}

// allow efficient initialization from temporaries
explicit IPCAtomic(T&& v) : 
   value{v}
{
}
于 2021-12-06T09:37:45.047 回答