how to call this method

Hi,

I've split the code into 2 pieces. However I've failed to call the SetDate function. How do I call a method that's a part of a class but put outside of it? In the .cpp how do I call SetDate fx? I've tried a few different ways.

Date.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef DATE_H
#define DATE_H

class Date
{
private:
    int m_year;
    int m_month;
    int m_day;
public:
    Date(int year, int month, int day);

    void SetDate(int year, int month, int day);

    int getYear() { return m_year; }
    int getMonth() { return m_month; }
    int getDay()  { return m_day; }
};

#endif


Date.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Date.h"
#include<iostream>
// Date constructor
Date::Date(int year, int month, int day)
{
    SetDate(year, month, day);
}

// Date member function
void Date::SetDate(int year, int month, int day)
{
    m_month = month;
    m_day = day;
    m_year = year;
    std::cout<<"here";
}
int main()
{

	//Date date;
	return 0;
}


//no errors with Date date commented out
Last edited on
You forgot to pass arguments to the constructor.

1
2
3
4
5
int main()
{
	Date date(2016, 8, 1);
	return 0;
}
Line 20: You're trying to call Date's default constructor. However, you don't have a default constructor.
closed account (E0p9LyTq)
date.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef DATE_H
#define DATE_H

using USHORT = unsigned short;

class Date
{
public:
   Date();
   Date(USHORT year, USHORT month, USHORT day);

   void SetDate(USHORT year, USHORT month, USHORT day);

   USHORT getYear() { return m_year; }
   USHORT getMonth() { return m_month; }
   USHORT getDay()  { return m_day; }

private:
   USHORT m_year;
   USHORT m_month;
   USHORT m_day;
};

#endif 


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

// default Date constructor
Date::Date()
{
   SetDate(1970, 1, 1);
}

// Date constructor
Date::Date(USHORT year, USHORT month, USHORT day)
{
   SetDate(year, month, day);
}

// Date member function
void Date::SetDate(USHORT year, USHORT month, USHORT day)
{
   m_month = month;
   m_day = day;
   m_year = year;
   // std::cout<<"here";
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "date.h"

int main ()
{
   Date date1;

   std::cout << "The first date is "
      << date1.getDay() << "/" << date1.getMonth() << "/" << date1.getYear() << ".\n";

   Date date2(2016, 8, 1);

   std::cout << "The second date is "
      << date2.getDay() << "/" << date2.getMonth() << "/" << date2.getYear() << ".\n";
}


The first date is 1/1/1970.
The second date is 1/8/2016.
closed account (E0p9LyTq)
If you wanted to get fancy and output your class as if it were a Plain Old Data (POD) type, you can overload std::ostream's insertion operator (<<):

date.h:
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
#ifndef DATE_H
#define DATE_H

using USHORT = unsigned short;

class Date
{
public:
   Date();
   Date(USHORT year, USHORT month, USHORT day);

   void SetDate(USHORT year, USHORT month, USHORT day);

   friend std::ostream& operator<<(std::ostream& os, const Date obj);

   USHORT getYear() { return m_year; }
   USHORT getMonth() { return m_month; }
   USHORT getDay()  { return m_day; }

private:
   USHORT m_year;
   USHORT m_month;
   USHORT m_day;
};

#endif 


date.cpp:
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
#include<iostream>
#include "date.h"

// default Date constructor
Date::Date()
{
   SetDate(1970, 1, 1);
}

// Date constructor
Date::Date(USHORT year, USHORT month, USHORT day)
{
   SetDate(year, month, day);
}

std::ostream& operator<< (std::ostream& os, const Date obj) 
{
   return os << obj.m_day << "/" << obj.m_month << "/" << obj.m_year;
}

// Date member function
void Date::SetDate(USHORT year, USHORT month, USHORT day)
{
   m_month = month;
   m_day = day;
   m_year = year;
   // std::cout<<"here";
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "date.h"

int main ()
{
   Date date1;

   std::cout << "The first date is "
      << date1.getDay() << "/" << date1.getMonth() << "/" << date1.getYear() << ".\n";

   Date date2(2016, 8, 1);

   std::cout << "The second date is "
      << date2.getDay() << "/" << date2.getMonth() << "/" << date2.getYear() << ".\n";

   std::cout << "\nAgain, the second date is " << date2 << ".\n";
}


The first date is 1/1/1970.
The second date is 1/8/2016.

Again, the second date is 1/8/2016.


Hi,

I think you should also become familiar with the constructor having a member initialization list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Date constructor
Date::Date(const USHORT yearArg, const USHORT monthArg, const USHORT dayArg)
: // colon introduces initialiser list
year(yearArg),  // direct initialisation not assignment
month(monthArg), //same order as class delcaration
day(dayArg)          //make sure to initialise all of them
{
   //SetDate(year, month, day);
// do validation here, throw if there is a problem
// invariants - call private functions to keep this tidy
// valid range for  year
// valid range for  month
// valid range for  day
// Leap Year validation for month and day

// object is not deemed to have been created until the end of the constructor is reached
}


You can still have the SetDate function for when the date is changed after the object is created.

Having the SetDate function and calling it from the ctr seems like a good idea, but it is a little inefficient. Before the opening brace of the ctr, the compiler assigns default values for each member variable; if one then gives them values by assignment - that is where the inefficiency lies - it is happening twice and there are unnamed temporaries being created. The member initializer list avoids this.

http://en.cppreference.com/w/cpp/language/initializer_list

@FurryGuy

Not sure whether you were aware of this header file:
http://en.cppreference.com/w/cpp/header/cstdint

closed account (E0p9LyTq)
@TheIdeasMan,

I am aware of that C library header, I preferred in this instance to use an explicit POD type (unsigned short).
@TheIdeasMan, et al

Recently have been introduced to c++11 uniform initialization list for constructors. The problem at hand here was the constructor wasn't called. Thanks for the perspective on the greater issues and to everyone who contributed.
Topic archived. No new replies allowed.