How to return struct declared in the same class wi

2019-09-05 08:34发布

问题:

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)
{

}

回答1:

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;

    };