无法正确地从函数调用返回值main()

时间:2012-09-04 11:12:14

标签: c++ visual-c++

我正在尝试测试这个简单的函数,但是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);

};     

我遇到的问题是:         如果(Opt.status)

它不返回值1,而是取前面定义的默认值。为什么会这样?

2 个答案:

答案 0 :(得分:1)

您需要通过引用传递参数:

uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions& Opt) 
                                                                    //^

否则,会在函数中创建并修改Opt的副本,并且调用者将永远不会看到更改。

请注意:

return Opt.status, Opt.Year; 

不会以某种方式返回两个值。这是使用comma operator并返回值Opt.Year。但是,如果您通过引用传递Opt,则返回值是不需要的。

答案 1 :(得分:0)

最好还是定义一个返回TOptions的函数 TOptions Easy_Task::display3(...);并返回结构 请记住,您只能从函数中返回 ONE SINGLE返回值