Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c+中,如何从一个类调用函数并将其应用于另一个不同类的函数+;? 问题_C++_Oop_Methods - Fatal编程技术网

C++ 在c+中,如何从一个类调用函数并将其应用于另一个不同类的函数+;? 问题

C++ 在c+中,如何从一个类调用函数并将其应用于另一个不同类的函数+;? 问题,c++,oop,methods,C++,Oop,Methods,我试图从另一个类调用一个函数,并将其应用到另一个类的类似函数 问题的细节 我有一个名为User的类,还有一个名为RelationType的函数ComparedTo(User*aUser)const;。此函数必须从另一个名为DateType的类调用函数,该函数称为RelationType ComparedTo(DateType someDate) 代码 这是我解决这个问题的代码。我可能在其他地方出了问题,但我还没有收到错误,所以我希望我是对的 日期类型.h RelationType Compare

我试图从另一个类调用一个函数,并将其应用到另一个类的类似函数

问题的细节 我有一个名为User的类,还有一个名为RelationType的函数ComparedTo(User*aUser)const;。此函数必须从另一个名为DateType的类调用函数,该函数称为RelationType ComparedTo(DateType someDate)

代码 这是我解决这个问题的代码。我可能在其他地方出了问题,但我还没有收到错误,所以我希望我是对的

日期类型.h RelationType ComparedTo(DateType someDate);是公众的最底层

#include <string>
#include <fstream>
using namespace std;
//Declare a class to represent the Date ADT
enum RelationType { LESS, EQUAL, GREATER };

class DateType
{
public:
    DateType();
    void Initialize(int newMonth, int newDat, int newYear);
    int GetMonth() const;               //returns month
    int GetYear() const;                //returns year
    int GetDay() const;                 //returns day
    string GetMonthAsString() const;        //returns month as string
    DateType Adjust(int daysAway) const;
    RelationType ComparedTo(DateType someDate) const;
private:
    int year;
    int month;
    int day;
};
#包括
#包括
使用名称空间std;
//声明一个类来表示日期ADT
枚举关系类型{LESS,EQUAL,GREATER};
类日期类型
{
公众:
日期类型();
无效初始化(int newMonth、int newDat、int newYear);
int GetMonth()const;//返回月份
int GetYear()const;//返回年份
int GetDay()const;//返回日期
字符串getMontHassString()const;//以字符串形式返回月份
日期类型调整(int daysAway)常数;
RelationType ComparedTo(DateType someDate)常量;
私人:
国际年;
整月;
国际日;
};
DateType.cpp RelationType DateType::ComparedTo(DateType someDate)常量是倒数第二个函数

#include "DateType.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

DateType::DateType()
{
    year = 1980;
    month = 1;
    day = 1;
}
//Number of days in each month
static int daysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

//Names of months
static string conversionTable[] = { "Error", "January", "February", "March", "April", "May", "June", "July",
                                    "August", "September", "October", "November", "December" };

void DateType::Initialize(int newMonth, int newDay, int newYear)
/*
    Post: If newMonth, newDay, and newYear represent a valid date, 
        year is set to newYear;
        month is set to newMonth;
        day is set to newDay;
        otherwise a string exception is thrown, stating the first
        incorrect parameter.
*/
{
    if (newMonth < 1 || newMonth > 12)
        throw string("Month is invalid");
    else if (newDay < 1 || newDay > daysInMonth[newMonth])
        throw string("Day is invalid");
    else if (newYear < 1600)
        throw string("Year is invalid");
    year = newYear;
    month = newMonth;
    day = newDay;
}

int DateType::GetMonth() const
{
    return month;
}

string DateType::GetMonthAsString() const
{
    return conversionTable[month];
}

int DateType::GetYear() const
{
    return year;
}

int DateType::GetDay() const
{
    return day;
}

RelationType DateType::ComparedTo(DateType someDate) const
/*
    Pre: Self and aDate have been initialized.
    Post: Function Value = LESS, if self comes before aDate
                         = EQUAL, if self is the same as aDate
                         = GREATER, is self comes after aDate
*/
{
    if (year < aDate.year)
        return LESS;
    else if (year > aDate.year)
        return GREATER;
    else if (month < aDate.month)
        return LESS;
    else if (month > aDate.month)
        return GREATER;
    else if (day < aDate.day)
        return LESS;
    else if (day > aDate.day)
        return GREATER;
    else
        return EQUAL;
}

DateType DateType::Adjust(int daysAway) const
/*
    Pre: Self has been initialized
    Post: Function value = newDate daysAway from self
*/
{
    int newDay = day + daysAway;
    int newMonth = month;
    int newYear = year;
    bool finished = false;
    int daysInThisMonth;
    DateType returnDate;
    while (!finished)
    {
        daysInThisMonth = daysInMonth[newMonth];
        if (newMonth == 2)
            if (((newYear % 4 == 0) && !(newYear % 100 == 0)) || (newYear % 400 == 0))
                daysInThisMonth++;
        if (newDay <= daysInThisMonth)
            finished = true;
        else
        {
            newDay = newDay - daysInThisMonth;
            newMonth = (newMonth % 12) + 1;
            if (newMonth == 1)
                newYear++;
        }
    }

    returnDate.Initialize(newMonth, newDay, newYear);
    return returnDate;
}
#include "DateType.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

enum RelationType { LESS, EQUAL, GREATER };

class User
{
public:
    User();
    void Initialize(char firstName[6], char lastName[6], DateType date);
    RelationType ComparedTo(User* aUser) const;
    string ToString();
    DateType getDateOfBirth();
    string getFirstName();
private:
    char fName[6];
    char lName[6];
    DateType dateOfBirth;
};
#包括“DateType.h”
#包括
#包括
#包括
使用名称空间std;
DateType::DateType()
{
年份=1980年;
月=1;
日=1;
}
//每月的天数
静态int daysInMonth[]={0,31,28,31,30,31,30,30,31,31,30,31};
//月份名称
静态字符串可转换[]={“错误”、“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”,
“八月”、“九月”、“十月”、“十一月”、“十二月”};
void DateType::Initialize(int newMonth、int newDay、int newYear)
/*
Post:如果newMonth、newDay和newYear表示有效日期,
新年已定;
month设置为newMonth;
day设置为newDay;
否则将引发字符串异常,声明第一个
参数不正确。
*/
{
如果(新月份<1 | |新月份>12)
抛出字符串(“月份无效”);
else if(newDay<1 | | newDay>daysInMonth[newMonth])
抛出字符串(“日期无效”);
否则,如果(新年<1600)
抛出字符串(“年份无效”);
年=新年;
月份=新月份;
day=newDay;
}
int DateType::GetMonth()常量
{
返回月份;
}
字符串日期类型::GetMontHaString()常量
{
返回可转换[月];
}
int DateType::GetYear()常量
{
回归年;
}
int DateType::GetDay()常量
{
回归日;
}
RelationType DateType::ComparedTo(DateType someDate)常量
/*
Pre:Self和aDate已初始化。
Post:如果self位于aDate之前,则函数值=较小
=相等,如果self与aDate相同
=更大,自我在aDate之后
*/
{
如果(年份<当前年份)
回报少;
否则,如果(年份>当前年份)
回报更大;
其他如果(月<当月)
回报少;
否则如果(月>当月)
回报更大;
否则,如果(日<日)
回报少;
否则,如果(日期>截止日期)
回报更大;
其他的
回报相等;
}
DateType DateType::调整(int daysAway)常量
/*
Pre:Self已初始化
Post:函数值=来自自身的newDate daysAway
*/
{
int newDay=日+日;
int newMonth=月份;
int newYear=年;
bool finished=false;
当月的整数天;
日期类型返回日期;
当(!完成)
{
daysInThisMonth=daysInMonth[新月份];
如果(新月份==2)
如果((新年%4==0)和&!(新年%100==0))| |(新年%400==0))
当月的天数++;
if(newDay 6 | | firstName[6]<1)
抛出字符串(“名字无效”);
else if(lastName[6]>6 | | lastName[6]<1)
抛出字符串(“姓氏无效”);
fName[6]=名字[6];
lName[6]=姓氏[6];
出生日期=出生日期;
}
DateType用户::getDateOfBirth()
{
出生返回日期;
}
RelationType User::ComparedTo(User*aUser)const//如果我有问题,请返回到此
{
日期型aDate;
aDate=出生日期;
dateOfBirth.ComparedTo(DateType someDate);//我可能很接近idk
}
User.cpp问题 如您所见,我不知道如何调用ComparedTo(DateType someDate)函数。我的RelationType User::ComparedTo(User*aUser)常量的语法和逻辑都很混乱。我曾尝试查找类的朋友,看看这是否可行,但对我来说唯一有意义的是这个点。我是否遗漏了一些非常简单的东西

免责声明
这是我第一次在这里问问题。如果我把问题弄糊涂了,请原谅。

1)
DateType::ComparedTo
参数声明为
someDate
,但代码使用了
aDate
,但没有定义。2)
User::ComparedTo
使用
dateOfBirth.ComparedTo(aUser->dateOfBirth)
比较日期。3)
fName[6]=firstName[6];
这是错误的,因为它会超出缓冲区,即使没有超出缓冲区,也会复制一个字符,而不是整个数组。请随意删除您的问题不需要的所有函数。它会消除很多噪音,帮助人们关注一个问题。它还将不需要告诉我们如何找到感兴趣的函数,例如只有感兴趣的功能会保留下来。
#include "User.h"
#include "DateType.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

User::User()
{
    string fName = "none";
    string lName = "none";
    DateType dateOfbirth;
}

void User::Initialize(char firstName[6], char lastName[6], DateType date)
{
    if (firstName[6] > 6 || firstName[6] < 1)
        throw string("First name is invalid");
    else if (lastName[6] > 6 || lastName[6] < 1)
        throw string("Last name is invalid");
    fName[6] = firstName[6];
    lName[6] = lastName[6];
    dateOfBirth = date;
}

DateType User::getDateOfBirth()
{
    return dateOfBirth;
}

RelationType User::ComparedTo(User* aUser) const //come back to this if I have a problem
{
    DateType aDate;
    aDate = dateOfBirth;
    dateOfBirth.ComparedTo(DateType someDate); //I might be close idk
    

    

}