私のコードブロックで何か問題があったことに気付いた人はいませんか?このプログラムは、2つの日付を比較するテストプログラムです。作業中の関数は、呼び出し日が大きい場合は1を、呼び出す日付が-1より小さい場合は-1を、呼び出し日がパラメータの日付と等しい場合は0を返します。私のテストプログラム:私のコードブロックから正しい出力が得られないようです。
#include <cstdlib>
#include <iostream>
#include <string>
#include "date.h"
using namespace std;
//date is initialized in a month/day/year format.
int main(int argc, char* argv[])
{
string* d;
date d1(4,1,4);
date d4(4,4,4);
int greaterTest = d4.compareTo(d1);
int lessTest = d1.compareTo(d4);
cout << greaterTest << endl; //i believe these two lines are printing out a
cout << lessTest << endl; //location in memory
cout<<&d <<endl;
system("pause");
return EXIT_SUCCESS;
}
巨大なのcompareTo()関数は:私はその日(日付の二番目のパラメータ)を変更しようとすると、
int date::compareTo (date another_date)
{
if (this->year == another_date.year && this->month == month && this->day < another_date.day) //if both year and month are the same, test to see if day is less
{
return -1;
}
else if (this->year == another_date.year && this->month == month && this->day > another_date.day) //if both year and month are the same, test to see if day is greater
{
return 1;
}
else if (this->year == another_date.year && this->month > month) //if the years are the same, test to see if the invoking month is greater
{
return 1;
}
else if (this->year == another_date.year && this->month < month) //if the years are the same, test to see if the invoking month is less
{
return -1;
}
else if (this->year > another_date.year) //test to see if the invoking year is greater
{
return 1;
}
else if (this->year < another_date.year) //test to see if the invoking year is less
{
return -1;
}
else if(this-> year == another_date.year && this-> month == another_date.month //test if the dates are exactly the same
&& this-> day == another_date.day)
{
return 0;
}
//else{ return 15;} //if none are true, return 15
}
取得唯一の問題イムです。
質問を編集して、正しく比較されない日付を含めることができますか? –
なぜいくつかのif条件でanother_date.monthを使用しないのはなぜですか? StackOverflowでの対処にエラーがあります。それ以外の場合はエラーです。 – Aslan986