Error C2440: 'initializing': cannot convert from 'initializer-list'

#include<iostream>
using namespace std;
struct TDate
{
    int day, month, year;
    void Readfromkb()
    {
        cout << "\n ENTER DAY MONTH YEAR\n";
        cin >> day >> month >> year;
    }
    void print()
    {
        cout << day << month << year;
    }
    private:
        int ID;
        bool valid;

};
int main()
{
    TDate t1, t2,t3={ 1, 2, 3 };
    t1.Readfromkb();
    t1.print();
    cin.ignore();
    cin.get();
    return 0;

}

why am I getting error Error 1 C2440: "initializing": cannot convert from "initializer-list" to "TDate" and 2 IntelliSense: too many initialization values. When I delete bool valid and int ID, the programs work. Why is this so?

+4
source share
2 answers

You get an error because you are trying to initialize TDatefrom the aggregate initialization list. This cannot be done if this type has private members (for example, in your case IDand valid).

, int TDate t1, t2, t3(1, 2, 3).

+2

t3={ 1, 2, 3 };, TDate , :

TDate(int i, int i1, int i2);

, :

TDate::TDate(int i, int i1, int i2) {

}

, :

TDate t1 = TDate();
+1

Source: https://habr.com/ru/post/1613077/


All Articles