[英]C++ Switch statement to assign struct values
*我正在嘗試為使用 switch 語句選擇的任何鳥類類型分配一個結構 object 與來自不同結構的值。 但是,我收到了conflicting decoration
錯誤。 我該如何解決這個問題?
/**
temp and humidity control points
*/
struct chicken_config
{
char *node_type = "incubator";
char *sub_type = "chicken"; // set the type of incubator
int sub_type_id = 1;
int hot = 102; //set hot parameter
float optimum_temp = 99.5; // set optimum_temp temp to go to b4 turning off
float cold = 89.9; //set cold parameter
int high_hum = 65; //set high humidity parameter
int optimum_hum = 60; // set optimum humidity parameter to go to b4 turning off
int low_hum = 55; // set low humidity parameter
};
struct turkey_config
{
char *node_type = "incubator";
char *sub_type = "turkey"; // set the type of incubator
int sub_type_id = 2;
int hot = 102; //set hot parameter
float optimum_temp = 99.5; // set optimum_temp temp to go to b4 turning off
float cold = 89.9; //set cold parameter
int high_hum = 65; //set high humidity parameter
int optimum_hum = 60; // set optimum humidity parameter to go to b4 turning off
int low_hum = 55; // set low humidity parameter
};
struct peacock_config
{
char *node_type = "incubator";
char *sub_type = "peacock"; // set the type of incubator
int sub_type_id = 3;
int hot = 101; //set hot parameter
float optimum_temp = 99.5; // set optimum_temp temp to go to b4 turning off
float cold = 98.9; //set cold parameter
int high_hum = 65; //set high humidity parameter
int optimum_hum = 60; // set optimum humidity parameter to go to b4 turning off
int low_hum = 55; // set low humidity parameter
};
struct chameleon_config
{
char *node_type = "incubator";
char *sub_type = "chameleon"; // set the type of incubator
int sub_type_id = 4;
int hot = 81; //set hot parameter
float optimum_temp = 77.5; // set optimum_temp temp to go to b4 turning off
float cold = 76.5; //set cold parameter
int high_hum = 95; //set high humidity parameter
int optimum_hum = 85; // set optimum humidity parameter to go to b4 turning off
int low_hum = 75; // set low humidity parameter
};
// structure for current incubator configuration
struct current_config
{
char *node_type = "incubator";
char *sub_type; // set the type of incubator
int sub_type_id;
int hot; //set hot parameter
float optimum_temp; // set optimum_temp temp to go to b4 turning off
float cold; //set cold parameter
int high_hum; //set high humidity parameter
int optimum_hum; // set optimum humidity parameter to go to b4 turning off
int low_hum; // set low humidity parameter
} currentConfig;
///////////////////////////////////////// ////
以下是我遇到麻煩的地方。
// setup directed sub-type
void switch_sub_type(int sub_type)
{
switch (sub_type)
{
// assign the bird type params
case 1: chicken_config currentConfig;
case 2: turkey_config currentConfig;
case 3: peacock_config currentConfig;
case 4: chameleon_config currentConfig;
}
return;
}
任何建議或意見,將不勝感激。
您的代碼中有幾個相關問題
struct
概念似乎是錯誤的:您可以使用一組特定的參數定義單個struct
類型並創建該struct
的多個實例。 對於您的情況,您可以創建一個基本的 animal_config 結構,並為每個要包含在代碼中的動物創建一個實例。這樣,您可以創建通用配置:
struct animal_config
{
char node_type;
char sub_type; // set the type of incubator
int sub_type_id;
int hot = 102; //set hot parameter
float optimum_temp; // set optimum_temp temp to go to b4 turning off
float cold; //set cold parameter
int high_hum; //set high humidity parameter
int optimum_hum; // set optimum humidity parameter to go to b4 turning off
int low_hum; // set low humidity parameter
};
然后根據條件為其分配不同的值:
void switch_sub_type(int sub_type)
{
switch (sub_type)
{
// assign the bird type params
struct animal_config currentConfig;
case 1:
animal_config.node_type = "incubator";
animal_config.sub_type = "chicken";
animal_config.sub_type_id = 1;
animal_config.hot = 102;
...
};
break;
case 2:
...
}
return;
}
switch_sub_type
方法沒有返回任何內容或將 currentConfig 存儲在任何地方。 在 function 調用完成后,它將從堆棧中刪除。 按照我之前的示例,也許您想將currentConfig
object 返回給調用者:
struct animal_config switch_sub_type(int sub_type)
{
switch (sub_type)
{
// assign the bird type params
struct animal_config currentConfig;
case 1:
animal_config.node_type = "incubator";
animal_config.sub_type = "chicken";
animal_config.sub_type_id = 1;
animal_config.hot = 102;
...
};
break;
case 2:
...
}
return currentConfig;
}
break;
每個案例后的聲明。 否則,如果選擇了一個案例,那么它之后的所有案例也將被執行,從而產生(可能)不希望的行為。我有兩個建議:一個結構,可分割的變量; 和一個基礎 class,幾個子類。
根據您的代碼,每只鳥都可能是同一結構的不同實例。
struct Bird_Config
{
char *node_type;
char *sub_type; // set the type of incubator
int sub_type_id;
int hot; //set hot parameter
float optimum_temp; // set optimum_temp temp to go to b4 turning off
float cold; //set cold parameter
int high_hum; //set high humidity parameter
int optimum_hum; // set optimum humidity parameter to go to b4 turning off
int low_hum; // set low humidity parameter
};
int main()
{
Bird_Config chicken;
chicken.hot = 102;
Bird_Config turkey;
//...
return 0;
}
另一種實現可能是使用父/基 class 和許多子類:
struct Bird_Config
{
char *node_type = "incubator";
char *sub_type; // set the type of incubator
int sub_type_id;
int hot; //set hot parameter
float optimum_temp; // set optimum_temp temp to go to b4 turning off
float cold; //set cold parameter
int high_hum; //set high humidity parameter
int optimum_hum; // set optimum humidity parameter to go to b4 turning off
int low_hum; // set low humidity parameter
};
class Chicken_Config : public Bird_Config
{
Bird_Config::sub_type = "chicken";
//...
};
The inheritance model allows you to do something like this:
int main()
{
std::vector<Bird_Config *> configs;
Bird_Config * p_bird = new Chicken_Config;
configs.push_back(p_bird);
p_bird = new Turkey_Config;
configs.push_back(p_bird);
//...
for (int i = 0; i < configs.size(); ++i)
{
std::cout << "Bird Configuration["
<< i
<< "]: "
<< configs[i]->sub_type
<< "\n";
}
return 0;
}
在上面的示例中,您不需要根據配置類型進行switch
。 inheritance 機制(多態性)將為您處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.