我正在尝试测试这个简单的函数,但是 Opt.status、Opt.Year 值不会返回给 main()。为什么?请帮忙,因为我是 C++ 新手。我正在使用 Visual C++ 来执行这些代码。这是在我的 .cpp 文件中
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdio.h"
using namespace std;
int main()
{
Easy_Task obj_EasyTask;
TimeDateMonthOptions whatOptions=DATE;
TOptions Opt;
Opt.status=FALSE;
Opt.Year=1970;
printf("Enter code\n");
scanf("%d",&obj_EasyTask.code);
cout << "the code entered is: " << obj_EasyTask.code;
obj_EasyTask.display2(obj_EasyTask.code);
cout << "\nOutput: " << obj_EasyTask.show();
printf("\nEnter the options that you prefer\n");
scanf("%d",&whatOptions);
obj_EasyTask.display3(whatOptions, Opt);
cout << "\nOpt.Year: " << Opt.Year;
if(Opt.status)
{
obj_EasyTask.x=(Opt.Year)& 0x00FF;
obj_EasyTask.y=((Opt.Year)& 0xFF00)>>8;
cout << "\nX: " << obj_EasyTask.x;
cout << "\nY: " << obj_EasyTask.y;
obj_EasyTask.Result=(obj_EasyTask.x)*(obj_EasyTask.y);
}
char holdWindow;
std::cin >> holdWindow;
return 0;
}
uint16_t Easy_Task::display2(uint16_t code)
{
if(code==1)
{
c = 7;
}
else
{
c = 9;
}
return c;
}
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions Opt)
{
switch(whtOptions)
{
case 0:
case 1:
case 2:
case 3:
Opt.status=TRUE;
cout << "\nStatus1: " << Opt.status;
Opt.Year=1991;
cout << "\nYear1: " << Opt.Year;
break;
case 7:
Opt.status=FALSE;
cout << "\nStatus2: " << Opt.status;
Opt.Year=2013;
cout << "\nYear2: " << Opt.Year;
break;
default:
Opt.status=FALSE;
cout << "\nStatus3: " << Opt.status;
Opt.Year=2010;
cout << "\nYear3: " << Opt.Year;
break;
}
return Opt.status, Opt.Year;
}
在我的 .h 文件中,我的类定义如下:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef unsigned short uint16_t;
#define TRUE 1;
#define FALSE 0;
typedef struct TOptions
{
bool status;
uint16_t Year;
};
typedef enum
{
YEAR,
MONTH,
DATE,
HOURS,
MINUTES,
SECONDS,
HUNDRETHS,
UNDEFINED
}TimeDateMonthOptions;
class Easy_Task
{
public:
uint16_t code, c, x,y, Result;
uint16_t display2(uint16_t code);
uint16_t show()
{
return c;
};
uint16_t display3(TimeDateMonthOptions whatOptions, TOptions Opt);
};
我遇到的问题是: if(Opt.status)
它不返回 1 的值,而是采用前面定义的默认值。为什么会这样?