在C+中使用静态类成员+; 我刚刚开始学习C++,使用Bjarne Stroustrap的原著。本书在关于CLASE的章节中,有一个创建日期类的示例,该类具有以下接口: #pragma once #include <string> class Date { public: //public interface: typedef enum Month{Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec} Month; class Bad_date{ }; //exception class Date(int dd = 0, int mm = 1, int yy = 0); Date(int dd = 0, Month mm =Month(0), int yy =0); //functions for examining the Date: int day() const; Month month() const; int year() const; std::string string_rep() const; //string representation void char_rep(char s[]) const; //C-style string representation static void set_default(int, Month, int); static Date default_date; //functions for changing the Date: Date& add_year(int n); //add n years Date& add_month(int n); //add n months Date& add_day(int n); //add n days private: int d, m, y; //representation bool leapyear(int n); //check if year is a leapyear. };

在C+中使用静态类成员+; 我刚刚开始学习C++,使用Bjarne Stroustrap的原著。本书在关于CLASE的章节中,有一个创建日期类的示例,该类具有以下接口: #pragma once #include <string> class Date { public: //public interface: typedef enum Month{Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec} Month; class Bad_date{ }; //exception class Date(int dd = 0, int mm = 1, int yy = 0); Date(int dd = 0, Month mm =Month(0), int yy =0); //functions for examining the Date: int day() const; Month month() const; int year() const; std::string string_rep() const; //string representation void char_rep(char s[]) const; //C-style string representation static void set_default(int, Month, int); static Date default_date; //functions for changing the Date: Date& add_year(int n); //add n years Date& add_month(int n); //add n months Date& add_day(int n); //add n days private: int d, m, y; //representation bool leapyear(int n); //check if year is a leapyear. };,c++,static,C++,Static,当我调用构造函数时,我在编译时得到一个对'Date::default\u Date'的未定义引用错误。我在网上读到,这通常是静态变量被单位化时引起的,因此我尝试将该变量声明为: static Date default_date = 0; //or static Date default_date = NULL; 这两个声明都不起作用,它们都给了我另一个错误 invalid in-class initialization of static data member of non-integral

当我调用构造函数时,我在编译时得到一个
对'Date::default\u Date'的未定义引用
错误。我在网上读到,这通常是静态变量被单位化时引起的,因此我尝试将该变量声明为:

static Date default_date = 0; //or
static Date default_date = NULL;
这两个声明都不起作用,它们都给了我另一个错误

invalid in-class initialization of static data member of non-integral type 'Date'
'Date Date::default_date' has incomplete type
如何处理此错误?
谢谢

您必须这样定义变量:

Date Date::default_date(1, 1, 1980);

您应该将它放在
.cpp
文件中,而不是
.h
文件中,因为它是一个定义,如果它包含在头文件中,您可以多次定义它。

从.cpp文件中的实现中删除
static
关键字,并避免将其初始化为
0

Date.cpp:

Date Date::default_date(1, 1, 1970);

首先,如果您应该使用默认构造函数,
staticdatedefault\u-Date就可以了


其次,应该初始化对象default\u date,
date date::default\u date(1,11980)

在类外定义它们-
日期日期::默认日期在单个实现文件中。“Stroustrap…分类…”。。。另外,我没有完全那样做,而是使用了
静态日期默认值\u日期(1,1,92)
。这也很有效。谢谢。这个问题的重点是,这个成员是静态的,我不明白你的意思;您可以在静态成员变量的声明中使用
static
,但不能在定义中使用。@stefan我已经在注释中解释过了。注释位于//之后,介于//*之间*/
Date Date::default_date(1, 1, 1970);
// header
struct wtf{};
struct omg{ static wtf lol; /* declaration of lol */ };
// cpp
wtf omg::lol; // definition of lol

int main(){ omg().lol; /* test if you can instantiate omg and access lol */ }