- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
只是尝试按照自定进度的 C++ 书籍学习。
到目前为止,我创建的程序有一个 Date 对象,它有四个参数。用户填写的三个参数是,“月”,“日”,“年”。第四个参数是“Old”,也就是存放多少天。为此,我编写了一个函数来计算它存在了多少天,并将其设置为第四个参数。
这部分有效。它计算它已经存在了多少天。
这本书要我减去两个 Date 类型的对象,并找出它们之间的差异。为此,它特别要求我创建一个 (-) 的重载运算符,然后将两者相减。这是我有点卡住的地方。我认为我遇到问题的部分是用于访问它的调用。我继续收到“operator-”的“模糊重载”错误(操作数类型为“日期”和“日期”)。
它将两个对象都识别为日期。现在我只需要一点帮助来完成最后一段。
我曾尝试在 header 中使用几种不同的东西,但也许我没有正确地实现它们。
-friend Date operator - (Date &ob1, Date &ob2);
-Date &operator-();
-Date &operator-(Date);
-Date operator-(Date);
如果你能帮忙解决这个问题,那就太好了。我是新手,我已经在这个问题上花费了超过 12 个小时。
main.cpp
#include <iostream>
#include <time.h>
#include <ctime>
#include "Date.cpp" // Date class definition
using namespace std;
int main() {
unsigned int birthMonth = 0;
unsigned int birthDay = 0;
unsigned int birthYear = 0;
unsigned int dateToMonth = 0;
unsigned int dateToDay = 0;
unsigned int dateToYear = 0;
cout << "Enter birth month (1-12): ";
cin >> birthMonth;
cout << "Enter birth day (1-31): ";
cin >> birthDay;
cout << "Enter birth year (1900 - 2000): ";
cin >> birthYear;
Date birthDate (birthMonth, birthDay, birthYear);
Date tempDate (1, 1, 1, 1);
cout << "To which date would you like to calculate to?\nEnter Day month (1-12): ";
cin >> dateToMonth;
cout << "Enter Day to calculate it to (1-31): ";
cin >> dateToDay;
cout << "Enter Year to calculate it to: ";
cin >> dateToYear;
Date dateTo (dateToMonth, dateToDay, dateToYear);
pastDays(birthDate);
pastDays(dateTo);
cout << "\nHow many days ago is the birth date? " << birthDate.old << endl;
cout << "How many days ago is the secondary date? " << dateTo.old << endl;
// Here is where I get an error "ambiguous overload for "operator-' (operand types are 'Date' and 'Date')"
cout << tempDate = birthDate - dateTo << endl;
cout << tempDate.old;
}
日期.h
#ifndef DATE_H
#define DATE_H
#include <array>
#include <iostream>
class Date
{
friend std::ostream &operator<<( std::ostream &, const Date & );
public:
Date( int m = 1, int d = 1, int y = 1900, int o = 0 ); // default constructor
void setDate( int, int, int, int ); // set month, day, year
// friend Date operator - (Date &ob1, Date &ob2);
Date &operator-(); // Modified Line for Assignment
Date &operator-(Date); // Modified Line for Assignment
// Date operator-(Date);
void pastDays (Date);
static bool leapYear( int ); // is date in a leap year?
unsigned int month;
unsigned int day;
unsigned int year;
int old;
static const std::array< unsigned int, 13 > days; // days per month
}; // end class Date
#endif
日期.cpp
#include <iostream>
#include <string>
#include "Date.h"
#include <time.h>
#include <ctime>
#include <conio.h>
#include "Date.h" // Date class definition
using namespace std;
// initialize static member; one classwide copy
const array< unsigned int, 13 > Date::days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Date constructor
Date::Date( int month, int day, int year, int old )
{
setDate( month, day, year, old );
} // end Date constructor
// set month, day and year
void Date::setDate( int mm, int dd, int yy, int old )
{
if ( mm >= 1 && mm <= 12 )
month = mm;
else
throw invalid_argument( "Month must be 1-12" );
if ( yy >= 1900 && yy <= 2100 )
year = yy;
// test for a leap year
// if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
// if (dd >= 1 || dd <= 31)
// day == dd;
// }
// if (month == 4 || month == 6 || month == 9 || month == 11){
// if (dd >= 1 || dd <= 30)
// day == dd;
// }
// if (month == 2)
// if ( year % 4 != 0 ) {
// if (dd >= 1 || dd <= 29)
// day == dd;
// }
// if (month == 2)
// if ( year % 4 != 0 ) {
// if (dd >= 1 || dd <= 28)
// day == dd;
// }
// else {
// throw invalid_argument(
// "Day is out of range for current month and year" );
// }
if ( ( month == 2 && leapYear( year ) && dd >= 1 && dd <= 29 ) ||
( dd >= 1 && dd <= days[ month ] ) )
day = dd;
else
throw invalid_argument(
"Day is out of range for current month and year" );
} // end function setDate
void pastDays (Date &entryDay) {
//Creating Today date object
time_t t = time(0);
struct tm * now = localtime(&t);
int currentYear = now -> tm_year + 1900;
int currentMonth = now -> tm_mon + 1;
int currentDay = now -> tm_mday;
int birthMonth = entryDay.month;
int birthDay = entryDay.day;
int birthYear = entryDay.year;
//The variable that will be assigned to the old parameter, which can then be subtracted from another time.
int daysAgo = 0;
entryDay.old = 0;
cout << endl;
cout << "First" << daysAgo << endl;
cout << "BirthMonth: " << birthMonth << endl;
cout << "BirthDay: " << birthDay << endl;
cout << "BirthYear: " << birthYear << endl;
//Lowering days to 1, to make transition between years easier.
// while (birthDay > 1){
// birthDay--;
// daysAgo--;
// }
daysAgo = daysAgo - birthDay;
daysAgo++;
birthDay = 1;
cout << endl;
cout << "Second" << daysAgo << endl;
cout << "BirthMonth: " << birthMonth << endl;
cout << "BirthDay: " << birthDay << endl;
cout << "BirthYear: " << birthYear << endl;
//Lowering months to 1, to make transition between years easier.
while (birthMonth > 1){
if (birthMonth == 1 || birthMonth == 3 || birthMonth == 5 || birthMonth == 7 || birthMonth == 8 || birthMonth == 10 || birthMonth == 12){
birthMonth--;
daysAgo -= 31;
}
if (birthMonth == 4 || birthMonth == 6 || birthMonth == 9 || birthMonth == 11){
birthMonth--;
daysAgo -= 30;
}
if (birthMonth == 2)
if ( currentYear % 400 == 0 ||
( currentYear % 100 != 0 && currentYear % 4 == 0 ) ) {
birthMonth--;
daysAgo -= 29;
}
else {
birthMonth--;
daysAgo -= 28;
}
}
cout << endl;
cout << "Third" << daysAgo << endl;
cout << "BirthMonth: " << birthMonth << endl;
cout << "BirthDay: " << birthDay << endl;
cout << "BirthYear: " << birthYear << endl;
//Incrementing year to current year
while (birthYear < currentYear){
if ( currentYear % 400 == 0 ||
( currentYear % 100 != 0 && currentYear % 4 == 0 ) ) {
daysAgo = daysAgo + 366;
birthYear++;
}
else {
daysAgo = daysAgo + 365;
birthYear++;
}
}
cout << endl;
cout << "Fourth" << daysAgo << endl;
cout << "BirthMonth: " << birthMonth << endl;
cout << "BirthDay: " << birthDay << endl;
cout << "BirthYear: " << birthYear << endl;
// Incrementing to current month
while (birthMonth < currentMonth) {
if (birthMonth == 1 || birthMonth == 3 || birthMonth == 5 || birthMonth == 7 || birthMonth == 8 || birthMonth == 10 || birthMonth == 12){
birthMonth++;
daysAgo += 31;
}
if (birthMonth == 4 || birthMonth == 6 || birthMonth == 9 || birthMonth == 11){
birthMonth++;
daysAgo += 30;
}
if (birthMonth == 2)
if ( currentYear % 400 == 0 ||
( currentYear % 100 != 0 && currentYear % 4 == 0 ) ) {
birthMonth++;
daysAgo += 29;
}
else {
birthMonth++;
daysAgo += 28;
}
}
cout << endl;
cout << "Fifth" << daysAgo << endl;
cout << "BirthMonth: " << birthMonth << endl;
cout << "BirthDay: " << birthDay << endl;
cout << "BirthYear: " << birthYear << endl;
//Incrementing to current day, and adding the days to the daysAgo
while (birthDay < currentDay){
birthDay++;
daysAgo++;
}
cout << endl;
cout << "Sixth" << daysAgo << endl;
cout << "BirthMonth: " << birthMonth << endl;
cout << "BirthDay: " << birthDay << endl;
cout << "BirthYear: " << birthYear << endl;
//Assigning DaysAgo to input parameter.old
entryDay.old = daysAgo;
}
Date operator - (Date &date1, Date &date2)
{
Date temp;
temp.old = date1.old - date2.old;
if(temp.old < 0)
{
temp.old = temp.old * -1;
}
return(temp);
}
//Date operator-(Date birthDate)
////friend Distance operator - (Date &birthDate, Date &today)
// {
//// int birthMonth = birthDate.month;
//// int birthDay = birthDate.day;
//// int birthYear = birthDate.year;
//// int birthOld = 0;
//// Date temp (birthDate.month, birthDate.day, birthDate.year, birthDate.old);
//
//// pastDays(today);
//// pastDays(birthDate);
//
//// int currentMonth = today.month;
//// int currentDay = today.day;
//// int currentYear = today.year;
//
//// int date1 = pastDays(today);
//// int date2 = pastDays(birthDate);
// Date temp (birthDate.month, birthDate.day, birthDate.year);
//
//// int month, int day, int year, int old
//// temp.month = this -> month;
//// temp.month = this -> day;
//// temp.month = this -> year;
//// Date temp = *this;
// cout << temp.old;
//
// temp.old = *this -> old - birthDate.old;
// //Here I get "Error: Invalid use of 'this' in non-member function;
//
// return(temp);
//}
bool Date::leapYear( int testYear )
{
if ( testYear % 400 == 0 ||
( testYear % 100 != 0 && testYear % 4 == 0 ) )
return true; // a leap year
else
return false; // not a leap year
} // end function leapYear
// overloaded output operator
ostream &operator<<( ostream &output, const Date &d )
{
static string monthName[ 13 ] = { "", "January", "February",
"March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
output << monthName[ d.month ] << ' ' << d.day << ", " << d.year;
return output; // enables cascading
} // end function operator<<
最佳答案
使用复合赋值运算符(例如-=
)定义您的二元算术运算符(例如-) .你可能想看看这个 binary arithmetic operator引用:
Date& operator-=(const X& rhs) {
/* subtraction of rhs to *this takes place here */
this->year - rhs.year; // This is the easy one
/* Here you will have to play around with this->month and this->day */
return *this; // return the result by reference
}
friend Date operator-(Date lhs, const Date& rhs) {
lhs -= rhs; // use compound assignment
return lhs; // return the result by value
}
关于C++ 重载运算符 - 减去相同类型的两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43065790/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!