Structure in C++

Need help here, is there something wrong with my code?

Here is the code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    struct data_date
    {

        int date ;
        int month  ;
        int year   ;
    };

    data_date date_birth;

    date_birth.date = 1;
    date_birth.month  = 9;
    date_birth.year  = 1999;
    cout << date.birth << endl;

    return 0;
}


Thanks in advance :)
Did you try compiling/running it?
Yes I do

the errors log is

error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|
What are you trying to display on line 21?
There are multiple things wrong with your code:

-the struct must be declared outside of the main() EDIT: pre c++11 whoops
-you can not cout date.birth, because you don't have an object called 'date' (also, even if you did, you'd have to override the '<<' operator).
Instead, write cout << date_birth.year << "." << date_birth.month << "." << date_birth.date << "." << endl;
Last edited on
-the struct must be declared outside of the main()


really?
C++11 supports local struct, class declarations.
you can not cout date.birth, because you don't have an object called 'date' (also, even if you did, you'd have to override the '<<' operator).


So I must change "date" into "data", right?
What are you trying to print? You have no class called date with a member birth, so perhaps you meant an _? It which case you simply output an entire structure like that; you'll need to output the individual elements or overload the << operator.
Topic archived. No new replies allowed.