0

I'm trying to create a copy constructor for a Gregorian date to Julian date calculator.

myDate.cpp (minimal)

#include "stdafx.h"
#include "myDate.h"

        int month = 0, day = 0, year = 0;

        myDate::myDate()
        {
            month = 5; day = 11; year = 1959; // Default date
        }

        myDate::myDate(int M, int D, int Y)
        {
            month = M; day = D; year = Y;
        }

        myDate::myDate(const myDate& D)
        {
            month = D.month;
            day = D.day;
            year = D.year;
        }

I get the errors 'month' is not a member of 'myDate' (same for day and year) and class "myDate" has no member "month" (same for day and year). What is the correct syntax for creating a copy constructor? This is the syntax I have seen widely used. Any help is greatly appreciated!

Here is the full header:

myDate.h

#ifndef MYDATE_H
#define MYDATE_H

#include <string>
using namespace std;

class myDate
{
    myDate();
    myDate(int M, int D, int Y);
    myDate(const myDate &D);
    string dayName();
    int getMonth();
    int getDay();
    int getYear();
    int Greg2Julian(int M, int D, int Y);
    void Julian2Greg(int JD, int& J, int& K, int& I);
    void display();
    void increaseDate(int N);
    void decreaseDate(int N);
    int daysBetween(myDate D);
    int dayOfYear();

};
#endif

and the full cpp:

myDate.cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "myDate.h"
using namespace std;


        int month = 0, day = 0, year = 0;
        string months[12] = { "January", "February", "March", "April", "May", "June", "July", "September", "October", "November", "December" };
        int dayCount[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        string weekdays[7] = { "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday" };

        myDate::myDate()
        {
            month = 5, day = 11, year = 1959; // Default date
        }

        myDate::myDate(int M, int D, int Y)
        {
            int nM = M; int nD = D; int nY = Y;
            int JD = Greg2Julian(M, D, Y);
            Julian2Greg(JD, nM, nD, nY);
            if (nM == M && nD == D && nY == Y)
                month = M, day = D, year = Y;
            else
                month = 5, day = 11, year = 1959;
        }

        myDate::myDate(const myDate& D)
        {
            month = D.month;
            day = D.day;
            year = D.year;
        }

        int myDate::getMonth()
        {
            return month;
        }

        int myDate::getDay()
        {
            return day;
        }

        int myDate::getYear()
        {
            return year;
        }

        int myDate::Greg2Julian(int M, int D, int Y)
        {
            return D - 32075 + 1461 * (Y + 4800 + (M - 14) / 12) / 4 + 367 * (M - 2 - (M - 14) / 12 * 12) / 12 - 3 * ((Y + 4900 + (M - 14) / 12) / 100) / 4;
        }

        void myDate::Julian2Greg(int JD, int& J, int& K, int& I) //use reference variables
        {
            int L = JD + 68569;
            int N = 4 * L / 146097;
            L = L - (146097 * N + 3) / 4;
            I = 4000 * (L + 1) / 1461001;
            L = L - 1461 * I / 4 + 31;
            J = 80 * L / 2447;
            K = L - 2447 * J / 80;
            L = J / 11;
            J = J + 2 - 12 * L;
            I = 100 * (N - 49) + I + L;
        }

        void myDate::display()
        {
            cout << months[month-1] << " " << day << ", " << year;
        }

        void myDate::increaseDate(int N)
        {
            Julian2Greg(Greg2Julian(month, day, year) + N, month, day, year);
        }

        void myDate::decreaseDate(int N)
        {
            Julian2Greg(Greg2Julian(month, day, year) - N, month, day, year);
        }

        int myDate::daysBetween(myDate D)
        {
            return Greg2Julian(D.getMonth(), D.getDay(), D.getYear()) - Greg2Julian(month, day, year);
        }

        int myDate::dayOfYear()
        {
            int sum = 0;
            for (int i = 0; i < month; i++)
            {
                if (month > i + 1)
                    sum += dayCount[i];
                else
                    sum += day;
            }
            return sum;
        }

        string myDate::dayName()
        {
            int dayNum = Greg2Julian(month, day, year) %7;
            return weekdays[dayNum];
        }
WinnB
  • 1
  • 1
  • 1
    Please show the declaration of the `myDate` class in `myDate.h`. If the compiler says they are not members, then they really are not members. Also, in your `myDate()` and `myDate(int, int, int)` constructors, the statements `month = ..., day = ..., year = ...;` should be separated by semicolons instead of commas: `{ month = ...; day = ...; year = ...; }`, like they are in your `myDate(const myDate&)` copy constructor. – Remy Lebeau Feb 27 '18 at 17:59
  • 1
    Well they don't seem to be members, but global variables? You better create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. – Some programmer dude Feb 27 '18 at 18:01
  • 1
    Present your [MCVE] as instructed in the help centre. – Lightness Races in Orbit Feb 27 '18 at 18:03
  • And if all you have as members are those three variables you show as globals, then you don't need an explicit copy-constructor. The default compiler-generated copy-constructor will work fine. – Some programmer dude Feb 27 '18 at 18:03
  • 3
    Okay, well, it doesn't. There are no data members in that class.. – Lightness Races in Orbit Feb 27 '18 at 18:05
  • Thank you for the code, but what you are doing is not really object-oriented programming... see answer below. – JHBonarius Feb 27 '18 at 18:19

1 Answers1

2

Since the variables are global, all instances of the myDate class will (in a way) share the same variables. If you change month for one object, it will be changed for all objects.

That's why you get errors for e.g. D.month, because there is no member month in the myDate class.

If you want those three to be actual members of the class, then you need to do something like

class myDate
{
    int month, day, year;

public:
    // The rest of the class and its member functions...
};

And then remove the global variables from the source file.

Lastly I suggest you get a couple of good books to read, from the beginning.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621