在头文件中使用 int
Using int in a header file?
在过去的几个小时里,我一直在处理头文件,并且在输出存储在构造函数中的值时遇到问题。该值是一个 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;
我建议你不要以这种形式对日期进行编码,如果这是你正在做的事情。
相关文章:
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- C++ 中文件输入输出中的长长 v/s int
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 如何读取 TXT 文件并仅将 int 存储在 C 中
- 如何检测将文本文件读入 int 数组的新行
- C++快速将 int 数组内容转储到文本文件中
- 为什么我可以从C++文件中读取 int 值,但不能浮点?
- 如何将逗号分隔的文件读取为 2D int 数组?
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- C++将文件行分离为String和Int
- C ++ txt 文件成数组逐行字符串和 int 用于纸牌游戏
- 为什么当我尝试将char输入到int时,文件指针会卡住
- 如何使用fstream从.txt文件C++中取出int值
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- 利用输入文件中的数据(字符串和int)并在循环中利用
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 如何通过头文件中的函数初始化 const int 数组?
- 无法一次从二进制文件中读取一个 int
- 我可以对"int 文件 = open(path, flag);"做点什么吗?
- 使用 c++ 时从二进制文件中读取 int 不正确