【问题标题】:C++ non-static const member in class without a constructor没有构造函数的类中的 C++ 非静态 const 成员
【发布时间】:2015-10-02 09:24:41
【问题描述】:

我不断收到此错误:

警告:非静态 const 成员‘const char sict::Weather::_date [7]’ 在没有构造函数的类中 [-Wuninitialized]

我不明白。

main.cpp

#include <iostream>
#include <iomanip>
#include "Weather.h"
using namespace std;
namespace sict{

int main(){
    int n; 
    Weather* weather;

    cout << "Weather Data\n";
    cout << "=====================" << endl;
    cout << "Days of Weather: ";
    cin >> n;
    cin.ignore();
    weather = new Weather(n);

    for (int i = 0; i < n; i++){
        char date_description[7];
        double high = 0.0, low = 0.0;

        cout << "Enter date: ";
        cin >> date_description;
        cout << "Enter high: ";
        cin >> high;
        cout << "Enter low : ";
        cin >> low;
    }
    cout << endl;
    cout << "Weather report:\n";
    cout << "======================" << endl;

    for (int i = 0; i < n; i++){
        weather[i].display();
    }

    delete[] weather;

    return 0;
}
}

天气.cpp

#include <iostream>
#include <cstring>
#include <iomanip>
#include "Weather.h"

using namespace std;
namespace sict{

void weather::set(const char* date[6], double _high, double _low){
    strncpy(_date, date_description, 6);
    _high = high;
    _low = low;
}

void weather::display() const{
    cout << setw(6) << setfill('_') << left << setw(6) << _date << right << setw(6) << _high << _low << '_' << endl;
}
}

天气.h

#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&’

【问题讨论】:

    标签: c++


    【解决方案1】:

    编译器告诉你你的类中有一个const 成员。作为const,它的值只能在类构造函数中设置(并且永远不会改变)。

    但是该类没有构造函数,所以值是从不设置的。

    【讨论】:

    • "作为 const,它的值只能在类构造函数中设置(然后永远不会改变)。"也可以在类定义中初始化const char something[] = {...};
    【解决方案2】:

    您的班级中有一个 const 成员 Weather。在 C++ 中,非静态 const 成员只能在构造函数初始化列表中进行初始化(参见this post)。

    要解决此问题,您可以:

    • 将_data成员声明为static,这样就可以初始化like this了。
    • 为您的类 Weather 声明并定义一个构造函数,并在其中初始化 _data

    否则成员_data永远不会被初始化,所以使用它的值是Undefined Behavior

    如果你至少使用 C++11,你还可以在类定义中初始化非静态成员:

    class Weather{
        const char _date[7] = "mydate";
    }
    

    【讨论】:

      【解决方案3】:
      • 你应该在定义之前声明你的类。这就是您收到编译错误的原因。
      • 不要使用 const,因为您在 setter 中更新它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-16
        • 2018-04-02
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-29
        相关资源
        最近更新 更多