【问题标题】:Copy constructor syntax error: myclass has no member mymember复制构造函数语法错误:myclass 没有成员 mymember
【发布时间】:2018-02-27 17:58:37
【问题描述】:

我正在尝试为公历日期到儒略日期计算器创建一个复制构造函数。

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;
        }

我收到错误“月”不是“myDate”的成员(日和年相同),并且“myDate”类没有成员“月”(日和年相同)。创建复制构造函数的正确语法是什么?这是我看到广泛使用的语法。非常感谢任何帮助!

这是完整的标题:

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

以及完整的 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];
        }

【问题讨论】:

  • 请在myDate.h 中显示myDate 类的声明。如果编译器说他们不是成员,那么他们真的不是成员。此外,在myDate()myDate(int, int, int) 构造函数中,语句month = ..., day = ..., year = ...; 应该用分号而不是逗号分隔:{ month = ...; day = ...; year = ...; },就像它们在myDate(const myDate&amp;) 复制构造函数中一样。
  • 好吧,它们似乎不是成员,而是全局变量?您最好创建一个Minimal, Complete, and Verifiable Example 向我们展示。
  • 按照帮助中心的说明提交您的minimal reproducible example
  • 如果您作为成员拥有的只是您显示为全局变量的这三个变量,那么您不需要显式的复制构造函数。默认的编译器生成的复制构造函数可以正常工作。
  • 好吧,好吧,它没有。该类中没有数据成员..

标签: c++ copy-constructor


【解决方案1】:

由于变量是全局变量,myDate 类的所有 实例将(在某种程度上)共享相同的变量。如果您为一个对象更改month,它将为所有个对象更改。

这就是为什么您会遇到错误,例如D.month,因为在 myDate 类中没有成员 month

如果你想让这三个人成为班级的实际成员,那么你需要做类似的事情

class myDate
{
    int month, day, year;

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

然后从源文件中删除全局变量。

最后我建议你从一开始就得到a couple of good books to read

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2017-09-07
    相关资源
    最近更新 更多