0

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

}
2
  • 7
    There's a difference between struct {} X and struct X {}. Commented Dec 30, 2013 at 16:34
  • 1
    You declared variables anonymous structs and the names are Output_Parameters, and Input_Parameters. Commented Dec 30, 2013 at 16:36

1 Answer 1

4

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;

    }; 
Sign up to request clarification or add additional context in comments.

3 Comments

@LightnessRacesinOrbit LOL.. No no. I thought these definitions are self explanatory. I will add some explanation now.
I think if they were self-explanatory to the OP then he wouldn't have posted the question ;)
"using typedef before your structure will be useful if you don't want to use 'struct' keyword before name" You already don't have to do that in C++, and on a related note your C answer is actually wrong. Otherwise better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.