C++ C++;类中没有构造函数的非静态常量成员
我不断地发现这个错误: 警告:非静态常量成员'const char sict::Weather::_date[7]' 在没有构造函数的类中[-Wuninitialized] 我不明白 main.cpp 我的编译错误如下C++ C++;类中没有构造函数的非静态常量成员,c++,C++,我不断地发现这个错误: 警告:非静态常量成员'const char sict::Weather::_date[7]' 在没有构造函数的类中[-Wuninitialized] 我不明白 main.cpp 我的编译错误如下 Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized] Wea
Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized]
Weather.cpp:11:7: error: ‘weather’ has not been declared
Weather.cpp: In function ‘void sict::set(const char**, double, double)’:
Weather.cpp:12:11: error: ‘_date’ was not declared in this scope
Weather.cpp:12:18: error: ‘date_description’ was not declared in this scope
Weather.cpp:13:11: error: ‘high’ was not declared in this scope
Weather.cpp:14:10: error: ‘low’ was not declared in this scope
Weather.cpp: At global scope:
Weather.cpp:18:7: error: ‘weather’ has not been declared
Weather.cpp:18:26: error: non-member function ‘void sict::display()’ cannot have cv-qualifier
Weather.cpp: In function ‘void sict::display()’:
Weather.cpp:19:57: error: ‘_date’ was not declared in this scope
Weather.cpp:19:86: error: ‘_high’ was not declared in this scope
Weather.cpp:19:95: error: ‘_low’ was not declared in this scope
Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized]
In file included from w3_in_lab.cpp:15:0:
Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized]
w3_in_lab.cpp: In function ‘int sict::main()’:
w3_in_lab.cpp:29:27: error: no matching function for call to ‘sict::Weather::Weather(int&)’
w3_in_lab.cpp:29:27: note: candidates are:
Weather.h:8:8: note: sict::Weather::Weather()
Weather.h:8:8: note: candidate expects 0 arguments, 1 provided
Weather.h:8:8: note: sict::Weather::Weather(const sict::Weather&)
Weather.h:8:8: note: no known conversion for argument 1 from ‘int’ to ‘const sict::Weather&’
编译器告诉您类中有一个
const
成员。作为const
,它的值只能在类构造函数中设置(然后再也不能更改)
但是该类没有构造函数,因此永远不会设置值。您的类中有一个const成员
Weather
。在C++中,非静态成员只能在构造函数初始化列表中初始化(参见)。
要解决此问题,您可以:
- 将_数据成员声明为
,这样可以初始化它静态
- 为类声明并定义构造函数,并在其中初始化数据
\u data
永远不会初始化,因此无法使用其值
如果您至少使用C++11,还可以在类定义中初始化非静态成员:
class Weather{
const char _date[7] = "mydate";
}
- 您应该在定义之前声明类。这就是为什么会出现编译错误
- 不要在setter中更新const
“作为常量,它的值只能在类构造函数中设置(然后永不更改)。”它也可以在类定义
const char something[]={…}中初始化代码>
#ifndef SICT_WEATHER_H_
#define SICT_WEATHER_H_
namespace sict{
class Weather{
const char _date[7];
double _high;
double _low;
public:
void set(const char* _date[7], double _high, double _low);
void display() const;
};
}
#endif
Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized]
Weather.cpp:11:7: error: ‘weather’ has not been declared
Weather.cpp: In function ‘void sict::set(const char**, double, double)’:
Weather.cpp:12:11: error: ‘_date’ was not declared in this scope
Weather.cpp:12:18: error: ‘date_description’ was not declared in this scope
Weather.cpp:13:11: error: ‘high’ was not declared in this scope
Weather.cpp:14:10: error: ‘low’ was not declared in this scope
Weather.cpp: At global scope:
Weather.cpp:18:7: error: ‘weather’ has not been declared
Weather.cpp:18:26: error: non-member function ‘void sict::display()’ cannot have cv-qualifier
Weather.cpp: In function ‘void sict::display()’:
Weather.cpp:19:57: error: ‘_date’ was not declared in this scope
Weather.cpp:19:86: error: ‘_high’ was not declared in this scope
Weather.cpp:19:95: error: ‘_low’ was not declared in this scope
Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized]
In file included from w3_in_lab.cpp:15:0:
Weather.h:10:21: warning: non-static const member ‘const char sict::Weather::_date [7]’ in class without a constructor [-Wuninitialized]
w3_in_lab.cpp: In function ‘int sict::main()’:
w3_in_lab.cpp:29:27: error: no matching function for call to ‘sict::Weather::Weather(int&)’
w3_in_lab.cpp:29:27: note: candidates are:
Weather.h:8:8: note: sict::Weather::Weather()
Weather.h:8:8: note: candidate expects 0 arguments, 1 provided
Weather.h:8:8: note: sict::Weather::Weather(const sict::Weather&)
Weather.h:8:8: note: no known conversion for argument 1 from ‘int’ to ‘const sict::Weather&’
class Weather{
const char _date[7] = "mydate";
}