简体   繁体   English

没有构造函数的类中的C ++非静态const成员

[英]C++ non-static const member in class without a constructor

I keep getting this error: 我一直收到这个错误:

warning: non-static const member 'const char sict::Weather::_date [7]' in class without a constructor [-Wuninitialized] 警告:没有构造函数的类中的非静态const成员'const char sict :: Weather :: _ date [7]'[-Wuninitialized]

And i don't understand it. 我不明白。

main.cpp 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;
}
}

Weather.cpp Weather.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;
}
}

Weather.h Weather.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

My compilation errors are as follows 我的编译错误如下

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

The compiler is telling you that you have a const member in your class. 编译器告诉您类中有const成员。 Being const , its value can only be set in the class constructor (and then never changed). 作为const ,它的值只能在类构造函数中设置(然后永远不会更改)。

But the class doesn't have a constructor, so the value is never set. 但是该类没有构造函数,因此永远不会设置该值。

You have a const member in your class Weather . 你的班级Weather有一个const成员。 In C++, non-static const members can only be initialized in the constructor initialization list (see this post ). 在C ++中,非静态const成员只能在构造函数初始化列表中初始化(参见本文 )。

To solve this issue you can either: 要解决此问题,您可以:

  • Declare the _data member as static , this way you can initialize it like this . 将_data成员声明为static ,这样就可以像这样初始化它。
  • Declare and define a constructor for your class Weather and initlialize _data in it. 声明和定义构造函数为类Weather和initlialize _data在里面。

Otherwise the member _data is never initialized, so using its value is Undefined Behavior . 否则,成员_data永远不会被初始化,因此使用其值是Undefined Behavior

If you are using at least C++11, you can also initialize non-static members in the class definition: 如果您至少使用C ++ 11,还可以在类定义中初始化非静态成员:

class Weather{
    const char _date[7] = "mydate";
}
  • you should declare your class before definition. 你应该在定义之前声明你的课程。 That's why you are getting compilation errors. 这就是你得到编译错误的原因。
  • and dont use const as you are updating it in your setter. 并且不要使用const,因为您在setter中更新它。

头

主要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++中初始化类中的非静态const变量? - how to initialize a non-static const variable in class in C++? C ++,在类构造函数中传递非静态函数指针 - C++, Passing non-static function pointer in a class constructor uninit_member:非静态类成员m_wszParams不在此构造函数或其在C ++中调用的任何函数中初始化 - uninit_member: Non-static class member m_wszParams is not initialized in this constructor nor in any functions that it calls in C++ 使用 const_cast 在类中分配非静态常量成员 - assign non-static const member in class with const_cast 为什么在没有类实例的情况下可以在编译时访问非常量,非静态成员? - Why can a non-const, non-static member be accessed at compile time without an instance of the class? 将 const char[] 初始化为非静态 class 成员 - initialize const char[] as non-static class member 指向模板类的非静态成员函数的C ++函数指针(类成员) - C++ function pointer (class member) to non-static member function of a template class 父类“无效使用非静态数据成员”的访问成员C ++ - Accessing member of parent class “Invalid use of non-static data member” C++ C ++函数指针(类成员)到非静态成员函数 - C++ function pointer (class member) to non-static member function 类 C++ 中的多线程非静态 - Multithreading non-static in class C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM