遇到一个很古怪的问题,C++类static const成员的初始化

时间:2022-09-09 22:47:38

在我的文件里有这class NFDuration, NFDuration.h里是这样的:

 

// A Duration represents the elapsed time between two instants
// as an int64 nanosecond count. The representation limits the
// largest representable duration to approximately 290 years.
class NFDuration
{
public:
    static const int64_t kNanosecond; // = 1LL
    static const int64_t kMicrosecond;// = 1000
    static const int64_t kMillisecond;// = 1000 * kMicrosecond
    static const int64_t kSecond; // = 1000 * kMillisecond
    static const int64_t kMinute; // = 60 * kSecond
    static const int64_t kHour; // = 60 * kMinute
public:
    NFDuration();
    explicit NFDuration(const struct timeval& t);
    explicit NFDuration(int64_t nanoseconds);
    explicit NFDuration(int nanoseconds);
    explicit NFDuration(double seconds);

    // Nanoseconds returns the duration as an integer nanosecond count.
    int64_t Nanoseconds() const;

    // These methods return double because the dominant
    // use case is for printing a floating point number like 1.5s, and
    // a truncation to integer would make them not useful in those cases.

    // Seconds returns the duration as a floating point number of seconds.
    double Seconds() const;

    double Milliseconds() const;
    double Microseconds() const;
    double Minutes() const;
    double Hours() const;

    struct timeval TimeVal() const;
    void To(struct timeval* t) const;

    bool IsZero() const;
    bool operator<(const NFDuration& rhs) const;
    bool operator<=(const NFDuration& rhs) const;
    bool operator>(const NFDuration& rhs) const;
    bool operator>=(const NFDuration& rhs) const;
    bool operator==(const NFDuration& rhs) const;

    NFDuration operator+=(const NFDuration& rhs);
    NFDuration operator-=(const NFDuration& rhs);
    NFDuration operator*=(int ns);
    NFDuration operator/=(int ns);

private:
    int64_t ns_; // nanoseconds
};

#include "NFDuration.in.h"

 

 在NFDuration.in.h里

#pragma once

inline NFDuration::NFDuration()
    : ns_(0)
{
}

inline NFDuration::NFDuration(const struct timeval& t)
    : ns_(t.tv_sec * kSecond + t.tv_usec * kMicrosecond)
{
}

inline NFDuration::NFDuration(int64_t nanoseconds)
    : ns_(nanoseconds)
{
}

inline NFDuration::NFDuration(int nanoseconds)
    : ns_(nanoseconds)
{
}

inline NFDuration::NFDuration(double seconds)
    : ns_((int64_t)(seconds * kSecond))
{
}

inline int64_t NFDuration::Nanoseconds() const
{
    return ns_;
}

inline double NFDuration::Seconds() const
{
    return double(ns_) / kSecond;
}

inline double NFDuration::Milliseconds() const
{
    return double(ns_) / kMillisecond;
}

inline double NFDuration::Microseconds() const
{
    return double(ns_) / kMicrosecond;
}

inline double NFDuration::Minutes() const
{
    return double(ns_) / kMinute;
}

inline double NFDuration::Hours() const
{
    return double(ns_) / kHour;
}

inline bool NFDuration::IsZero() const
{
    return ns_ == 0;
}

inline struct timeval NFDuration::TimeVal() const
{
    struct timeval t;
    To(&t);
    return t;
}

inline void NFDuration::To(struct timeval* t) const
{
    t->tv_sec = (long)(ns_ / kSecond);
    t->tv_usec = (long)(ns_ % kSecond) / (long)kMicrosecond;
}

inline bool NFDuration::operator<(const NFDuration& rhs) const
{
    return ns_ < rhs.ns_;
}

inline bool NFDuration::operator<=(const NFDuration& rhs) const
{
    return ns_ <= rhs.ns_;
}

inline bool NFDuration::operator>(const NFDuration& rhs) const
{
    return ns_ > rhs.ns_;
}

inline bool NFDuration::operator>=(const NFDuration& rhs) const
{
    return ns_ >= rhs.ns_;
}

inline bool NFDuration::operator==(const NFDuration& rhs) const
{
    return ns_ == rhs.ns_;
}

inline NFDuration NFDuration::operator+=(const NFDuration& rhs)
{
    ns_ += rhs.ns_;
    return *this;
}

inline NFDuration NFDuration::operator-=(const NFDuration& rhs)
{
    ns_ -= rhs.ns_;
    return *this;
}

inline NFDuration NFDuration::operator*=(int n)
{
    ns_ *= n;
    return *this;
}

inline NFDuration NFDuration::operator/=(int n)
{
    ns_ /= n;
    return *this;
}

 

在NFDuration.cpp里是这样初始化的:

#include "NFDuration.h"

const int64_t NFDuration::kNanosecond = 1LL;
const int64_t NFDuration::kMicrosecond = 1000 * kNanosecond;
const int64_t NFDuration::kMillisecond = 1000 * kMicrosecond;
const int64_t NFDuration::kSecond = 1000 * kMillisecond;
const int64_t NFDuration::kMinute = 60 * kSecond;
const int64_t NFDuration::kHour = 60 * kMinute;

 3个文件这样写,在windows,linux系统上使用后,编译都没有问题,但是我觉得NFDuration.in.h这个文件麻烦,就把NFDuration.in.h里的东西移到了NFDuration.h里

class NFDuration
{
public:
    static const int64_t kNanosecond; // = 1LL
    static const int64_t kMicrosecond;// = 1000
    static const int64_t kMillisecond;// = 1000 * kMicrosecond
    static const int64_t kSecond; // = 1000 * kMillisecond
    static const int64_t kMinute; // = 60 * kSecond
    static const int64_t kHour; // = 60 * kMinute
public:
    NFDuration();
    explicit NFDuration(const struct timeval& t);
    explicit NFDuration(int64_t nanoseconds);
    explicit NFDuration(int nanoseconds);
    explicit NFDuration(double seconds);

    // Nanoseconds returns the duration as an integer nanosecond count.
    int64_t Nanoseconds() const;

    // These methods return double because the dominant
    // use case is for printing a floating point number like 1.5s, and
    // a truncation to integer would make them not useful in those cases.

    // Seconds returns the duration as a floating point number of seconds.
    double Seconds() const;

    double Milliseconds() const;
    double Microseconds() const;
    double Minutes() const;
    double Hours() const;

    struct timeval TimeVal() const;
    void To(struct timeval* t) const;

    bool IsZero() const;
    bool operator<(const NFDuration& rhs) const;
    bool operator<=(const NFDuration& rhs) const;
    bool operator>(const NFDuration& rhs) const;
    bool operator>=(const NFDuration& rhs) const;
    bool operator==(const NFDuration& rhs) const;

    NFDuration operator+=(const NFDuration& rhs);
    NFDuration operator-=(const NFDuration& rhs);
    NFDuration operator*=(int ns);
    NFDuration operator/=(int ns);

private:
    int64_t ns_; // nanoseconds
};

inline NFDuration::NFDuration()
    : ns_(0)
{
}

inline NFDuration::NFDuration(const struct timeval& t)
    : ns_(t.tv_sec * kSecond + t.tv_usec * kMicrosecond)
{
}

inline NFDuration::NFDuration(int64_t nanoseconds)
    : ns_(nanoseconds)
{
}

inline NFDuration::NFDuration(int nanoseconds)
    : ns_(nanoseconds)
{
}

inline NFDuration::NFDuration(double seconds)
    : ns_((int64_t)(seconds * kSecond))
{
}

inline int64_t NFDuration::Nanoseconds() const
{
    return ns_;
}

inline double NFDuration::Seconds() const
{
    return double(ns_) / kSecond;
}

inline double NFDuration::Milliseconds() const
{
    return double(ns_) / kMillisecond;
}

inline double NFDuration::Microseconds() const
{
    return double(ns_) / kMicrosecond;
}

inline double NFDuration::Minutes() const
{
    return double(ns_) / kMinute;
}

inline double NFDuration::Hours() const
{
    return double(ns_) / kHour;
}

inline bool NFDuration::IsZero() const
{
    return ns_ == 0;
}

inline struct timeval NFDuration::TimeVal() const
{
    struct timeval t;
    To(&t);
    return t;
}

inline void NFDuration::To(struct timeval* t) const
{
    t->tv_sec = (long)(ns_ / kSecond);
    t->tv_usec = (long)(ns_ % kSecond) / (long)kMicrosecond;
}

inline bool NFDuration::operator<(const NFDuration& rhs) const
{
    return ns_ < rhs.ns_;
}

inline bool NFDuration::operator<=(const NFDuration& rhs) const
{
    return ns_ <= rhs.ns_;
}

inline bool NFDuration::operator>(const NFDuration& rhs) const
{
    return ns_ > rhs.ns_;
}

inline bool NFDuration::operator>=(const NFDuration& rhs) const
{
    return ns_ >= rhs.ns_;
}

inline bool NFDuration::operator==(const NFDuration& rhs) const
{
    return ns_ == rhs.ns_;
}

inline NFDuration NFDuration::operator+=(const NFDuration& rhs)
{
    ns_ += rhs.ns_;
    return *this;
}

inline NFDuration NFDuration::operator-=(const NFDuration& rhs)
{
    ns_ -= rhs.ns_;
    return *this;
}

inline NFDuration NFDuration::operator*=(int n)
{
    ns_ *= n;
    return *this;
}

inline NFDuration NFDuration::operator/=(int n)
{
    ns_ /= n;
    return *this;
}

这个时候在wondows上使用后编译时没有问题的, 但是在Linux上老是报static const成员没有定义的错误,  于是我把初始化写的类里面, 把NFDuration.cpp里的东西删掉, NFDuration.h变成了这样

 

class NFDuration
{
public:
    static const int64_t kNanosecond = 1LL;
    static const int64_t kMicrosecond = 1000;
    static const int64_t kMillisecond = 1000 * kMicrosecond;
    static const int64_t kSecond = 1000 * kMillisecond;
    static const int64_t kMinute = 60 * kSecond;
    static const int64_t kHour = 60 * kMinute;
.........................
其余与上面一样

然后就发现,在Linux上是没有问题了,但是在windows上又报static const成员没有定义, 这是咋回事啊,网上搜索也没有找到答案,最后只好又恢复成文字刚开始3个文件的样子, 貌似只有这样才没有问题,两个系统都能编译,只是原因想不通

找了很久终于找到原因了,娘的啊。。。。。。。。。。。。。。。。。。。。。。。。

终于找到原因了,所有的问题都找错了,不管怎么样写其实都没有问题, 真正的问题是timeval这个数据结构的,没法支持跨平台,而linux系统的编译器老是误报, 没有吧问题真正的原因暴露出来,timeval我也不知道怎么做到快平台,最后只好加上libevent的头文件,

解决了这个问题。 本来想删掉这文章的,想想留住纪念吧!!