![](/img/trans.png)
[英]How to Return struct in a class when gthe struct is declared outside the class?
[英]How to return struct declared in the same class with function?
我的頭文件中有一個類聲明,如下所示。 一個函數將結構之一用作輸入,將另一個作為返回參數。 關鍵是當我以這種方式使用編譯器給我錯誤時。
請問是什么原因? 任何想法表示贊賞。
#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)
{
}
有三種方法可以解決您的問題。
A. struct struct_name {};
->此聲明結構稱為“ structure_name”
B. typedef struct {}struct_name;
->如果您不想在名稱前使用' struct
'關鍵字,則在結構前使用typedef
會很有用。
C.在函數原型中使用struct
關鍵字。
struct Output_Parameters FindExit(struct Input_Parameters input);
例如:
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;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.