A Function inside a declared namespace will not work.

1. I have this class declared inside a namespace and is in a header file called "date.h" as shown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdio>
#include <ctime> 
#include <cstdlib> 
#include <iostream> 
using namespace std;

#define highestDate {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
#define highestMonth 12;
#define defaultMonth 1;

namespace JuanGilbertoSanchez
{ 
    //CLass declaration of class date
    class date
    {
    //Variable declaration accesible only by the function of the class
    private: 
        int dateMonth;
        int dateDay;
        int dateYear;
    //Public class function accesible 
    public:
        date();
        date(int, int, int); 
        int getMonth() {return dateMonth;}
        int getDay()   {return dateDay;}
        int getYear()  {return dateYear;}
        void display();
    };
    
    //2 functions Used by C++ programming
    void static ErrorAndExit()
    {
        cerr << "Data is Under/over the allowed Value"; 
        exit(EXIT_FAILURE);
    }

    //display the date 
    void date::display()
    {
        cout << "The date is "  << dateMonth << '/' << dateDay << '/' << dateYear <<'\n';
    } 

    //default constructor sets the month, day, and year to the current date
    date::date()
    {
        time_t rawtime;
        struct tm * timeinfo;
        time (&rawtime);
        timeinfo = localtime( &rawtime);
        dateMonth = timeinfo->tm_mon;
        dateDay = timeinfo->tm_mday;
        dateYear = timeinfo->tm_year;
    }
    // class date constructor, the array represents the maximumdate of each month,
    // leap year is not considered here.
    date::date(int month, int day, int year)
    {
        // Variable declaration
        int maximumDate[] = highestDate; 
        int maximunMonth = highestMonth;
        // Check for valid value of month
        if (month > 0 && month <= maximunMonth)
            dateMonth = month;                              //load the private variable dateyMonth 
        else
        {
            dateMonth = defaultMonth;
            ErrorAndExit();
        }
        //check for valid value of day
        if (day > 0 && day <= maximumDate[dateMonth - 1]) 
            dateDay = day;                                  //load the private variable dateDay 
        else
            ErrorAndExit();
        //check for valid value of year
        if (year >= 0 )
            dateYear = year;                                //load the private variable dateYear
        else
            ErrorAndExit();
    }
}


2. I have my main in a file called "date.cpp" containing this.

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
#include <cstdlib>
#include "date.h"
using namespace std; 

int main()
{
    JuanGilbertoSanchez::date::date(); 
    JuanGilbertoSanchez::date::display();
    cin.get();
    return(EXIT_SUCCESS);
}


3. When I do this "JuanGilbertoSanchez::date::display();" the compiler complain.
the compiler suggest this "&JuanGilbertoSanchez::date::display;" which I did but when I execute it the command is bypassed so nothing is displayed.

What am I doing wrong?
You are trying to call a method without an object.
the compiler suggest this "&JuanGilbertoSanchez::date::display;" which I did but when I execute it the command is bypassed so nothing is displayed.
Yes, & in this case is an address operator. it's not assigned hence nothing happens.

As ne555 mentioned you need an object:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
#include <cstdlib>
#include "date.h"
using namespace std; 

int main()
{
    JuanGilbertoSanchez::date d; // Note that the constructor date() is called here;
    d.display(); // display() is called according to the object d
    cin.get();
    return(EXIT_SUCCESS);
}
&JuanGilbertoSanchez::date::display;
will give you the address of the member function.

You want to create an object of type date:
 
JuanGilbertoSanchez::date MyDate;


And then call the function using that object:
 
MyDate.display();
Making class is same as making a brand new user-defined "type". Compiler will treat the name of the new class as type name such as "int".

Because you have defined "class date" you have new type "date" to use.

1
2
3
4
5
// There is type int. Make a new variable with this.
int myInt; // Computer: OK, but I'm not gonna initialize this.

// There is type date in namespace JuanGilbertoSanchez. Make a new variable with this.
JuanGilbertoSanchez::date myDate; // Computer: OK, but let me initialize it with default constructor. 


Now you have variables, you can call functions.

operator+ is 'sort of' function that int type variable can call.
display() is function that date type variable can call.

1
2
myInt + 5; // OK. I'll 'sort of' call operator+ function defined for int type.
myDate.display() // OK. I'll call display function defined for date type. 
Last edited on
Topic archived. No new replies allowed.