#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 i'm getting Error 1 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'TDate' and 2 IntelliSense: too many initializer values. When I remove bool valid and int ID the programs works.Why is it so?
You are getting the error because you are trying to initialize a TDate
from an aggregate initialization list. This cannot be done when the type has private members (such as, in your case, ID
and valid
).
You can work around this by providing a constructor for your struct from three int
s and using TDate t1, t2, t3(1, 2, 3)
.
To do t3={ 1, 2, 3 };
you have to make constructor of TDate with takes three arguments, like this:
TDate(int i, int i1, int i2);
and implement it using:
TDate::TDate(int i, int i1, int i2) {
}
if you dont want to create constructor, then create object like this:
TDate t1 = TDate();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With