简体   繁体   中英

How to return struct declared in the same class with function?

I have a class declaration in my header file like shown below. One function uses one of the struct as input and the other one as return parameter. The point is when I use in this way compiler gives me error.

What would be the reason ? Any idea is appreciated.

#include <string>

using namespace std;

namespace My_Functions
{
    class My_Functions
    {

    public:
        struct {

            char input_a;
            int input_b;
            double input_c;
            double input_d;
            double input_e;
            double input_f;
            double input_g;

        } Input_Parameters;

        struct {

            char output_a;
            int output_b;
            double output_c;
            double output_d;
            int output_e;

        } Output_Parameters;

    public:
        Output_Parameters FindExit(Input_Parameters input);


    };

}

in cpp file

My_Functions::Output_Parameters My_Functions::FindExit(My_Functions::Input_Parameters input)
{

}

There are three ways to fix your problem.

A. struct struct_name {}; -> This declare structure called 'structure_name'

B. typedef struct {}struct_name; -> using typedef before your structure will be useful if you don't want to use ' struct ' keyword before name.

C. Use struct keyword in function prototype.

struct Output_Parameters FindExit(struct Input_Parameters input);

Ex for A:

    struct Input_Parameters {

        char input_a;
        int input_b;
        double input_c;
        double input_d;
        double input_e;
        double input_f;
        double input_g;

    } ;

    struct Output_Parameters{

        char output_a;
        int output_b;
        double output_c;
        double output_d;
        int output_e;

    }; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM