过去几个小时我一直在处理头文件,但在输出存储在构造函数中的值时遇到问题。该值是一个 int,但它不允许我存储任何大于 7 的数字,当我使用函数输出它时,它会出现一个完全不同的数字。我在一个头文件中完成这一切,并使用 .cpp 中的一个函数来输出数据。我是 C++ 的新手,所以这可能是一个业余错误。任何帮助将不胜感激!!
头文件----
#ifndef PATIENT_DEMO_CLASS
#define PATIENT_DEMO_CLASS
// system defined preprocessor statement for cin/cout operations
#include <iostream.h>
// programmer defined preprocessor statement for setreal operation
#include "textlib.h"
// programmer defined preprocessor statement for String
#include "tstring.h"
class PatientDemographicInformation
{
private:
int patientDateOfBirth;
public:
// constructor
PatientDemographicInformation(int dateOfBirth);
// returns the patient's age
int getPatientAge( );
};
PatientDemographicInformation::PatientDemographicInformation(int dateOfBirth)
{
patientDateOfBirth = dateOfBirth;
}
int PatientDemographicInformation::getPatientAge( )
{
return patientDateOfBirth;
}
#endif
.cpp ----
#include <iostream.h>
#include <tstring.h>
#include "PatientDemographicInformation.h"
int main( )
{
PatientDemographicInformation john(11161990);
cout << john.getPatientAge() << endl;
return 0;
}
最佳答案
纯粹是猜测,在这里。
在 C、C++ 和许多其他语言中,以 0 开头的整数是八进制;也就是说,它们是以 8 为基数而不是以 10 为基数。
如果你正在做类似的事情:
dateOfBirth = 070503;
那么它将被解释为八进制数(十进制为 28995)。由于八进制数只能包含数字 0-7,因此以下内容将是非法的:
dateOfBirth = 090503;
我建议您不要以这种形式对日期进行编码,如果您正在这样做的话。
关于c++ - 在头文件中使用 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038141/