c++ - 在头文件中使用 int?

标签 c++ function header-files

过去几个小时我一直在处理头文件,但在输出存储在构造函数中的值时遇到问题。该值是一个 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/

相关文章:

c++ - 获取dll中的应用程序路径

memory - Linux size命令,为什么bss和data部分不为零?

C++ - 位图的 10 种最流行的颜色

r - 如何在R中使用索引函数创建多列?

r - 将 data.frames 列表的列表转换为 R 中的单个 data.frame

c++ 在另一个类头文件中声明一个类

c++ - 使子窗口出现在任务栏中

TypeScript:当类型推断正确检测到类型时显式设置类型是不好的做法吗?

C++ 方法定义和变量声明

C++ 头文件重新定义错误