C++中的类到类数据类型转换
Class to Class Data Type Conversions in C++
我正在自学如何用C++编程并解决这个问题:
编写一个具有Date类和Julian类的C++程序。Julian类应该将日期表示为长整数。对于此程序,请在Date类中包含一个转换运算符函数,该函数使用提供的算法将
Date
对象转换为Julian
对象。通过转换2011年1月31日和2012年3月16日来测试您的程序,这两个日期对应于儒略历734533和734943。
因此,我们必须有一个Date
方法,它将参数转换为Julian
类。我知道这必须通过关键字operator
来完成。我写了一些代码,得到了以下错误消息:
ttt.cpp:34:7: error: incomplete result type 'Julian' in function definition
Date::operator Julian()
^
ttt.cpp:11:7: note: forward declaration of 'Julian'
class Julian; // Forward declaration
^
ttt.cpp:50:12: error: 'Julian' is an incomplete type
return Julian(long(365*year + 31*(month-1) + day + T - MP));
^
ttt.cpp:11:7: note: forward declaration of 'Julian'
class Julian; // Forward declaration
^
2 errors generated.
我不清楚这个错误信息是什么意思。我包含了一个正向声明,因为Julian
是在Date
之后定义的。我的代码在下面。如果有任何帮助,我将不胜感激。非常感谢。
#include <iostream>
#include <iomanip>
using namespace std;
/*
* Class to class conversion
*/
// CLASS DECLARATIONS=========================================================
class Julian; // Forward declaration
// "Date" Class Declaration------------------------------------------
class Date
{
private:
int month;
int day;
int year;
public:
Date(int=7, int=4, int=2012); // Constructor
operator Julian(); // Method to convert "Date" class to "Julian"
void showDate(); // print "Date"
};
// "Date" CLASS IMPLEMENTATION----------------------------
Date::Date(int mm, int dd, int yyyy)
{ // Constructor Method
month = mm;
day = dd;
year = yyyy;
}
Date::operator Julian()
{ // Method to convert "Date" class to "Julian"
int MP, YP, T;
if( month <=2 )
{
MP = 0;
YP = year - 1;
}
else
{
MP = int(0.4*month + 2.3);
YP = year;
}
T = int(YP/4) - int(YP/100) + int(YP/400);
return Julian(long(365*year + 31*(month-1) + day + T - MP));
}
void Date::showDate()
{
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100;
}
// "Julian" CLASS DECLARATION--------------------------------------------------------
class Julian
{
private:
int days;
public:
Julian(long=0); // Constructor
void show(); // Print julian date
};
// "Julian" Class Implementation----------------------------------------------------
Julian::Julian(long d)
{
days = d;
}
void Julian::show()
{
cout << days << endl;
}
int main()
{
Date a(1,31,2011);
Date b(3,16,2012);
Julian c, d;
c = Julian(a);
d = Julian(b);
a.showDate();
c.show();
cout << endl;
b.showDate();
d.show();
cout << endl;
return 0;
}
您需要在Date
类之前定义Julian
类。这里仅仅是正向声明是不起作用的,因为Date
类需要Julian
类的完整定义。
相关文章:
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- 从 cpp lib 调用函数时的数据类型转换
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- pybind11:Python 到 C++ 数据类型转换不起作用
- 尝试执行绕道附加,但无法将我的lua函数的数据类型转换为LPVOID
- 使用正确的数据类型转换java中的c++方法逻辑
- XLL由XLW制造,BOOST UBLAS MyMatrix数据类型转换为双**失败
- 将原始数据类型转换为空隙指针类型
- 为什么数据类型转换错误只出现在C++中而不出现在C中
- 正在尝试将time_t数据类型转换为(dd/mm/yyyy)格式C++
- C++中的类到类数据类型转换
- C++ 中的数据类型转换
- 无法从一种数据类型转换为相同的数据类型?
- 内置数据类型转换为用户定义的数据类型c++
- 如何将c数据类型转换为neon数据类型
- 将内置数据类型转换为std::string: memcpy vs snprintf
- c++中的动态数据类型转换
- 如何将Mat数据类型转换为无符号短指针
- 将c++的变量数据类型转换为c#