如何返回struct在同一个类中声明的函数? [英] How to return struct declared in the same class with function?
问题描述
我在我的头文件中有一个类声明,如下所示。一个函数使用一个struct作为输入,另一个作为返回参数。关键是当我以这种方式使用编译器给我错误。
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);
};
}
在cpp文件中
My_Functions::Output_Parameters My_Functions::FindExit(My_Functions::Input_Parameters input)
{
}
推荐答案
有三种方法可以解决您的问题。
There are three ways to fix your problem.
A。 struct struct_name {};
- >这个名为structure_name的声明结构
A. struct struct_name {};
-> This declare structure called 'structure_name'
typedef struct {} struct_name;
- >使用 typedef
之前的结构将是有用的,如果你不想使用' struct
'。
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。在函数原型中使用 struct
关键字。
C. Use struct
keyword in function prototype.
struct Output_Parameters FindExit(struct Input_Parameters input);
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;
};
这篇关于如何返回struct在同一个类中声明的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!