我有一个非常奇怪的问题。这是我的代码:头文件
namespace test
{
class LoadConfigData
{
struct control_params
{
char active[];
char suspended[];
char erased[];
}*ctrl_params;
bool loadConfig();
};
};
主文件
using namespace std;
using namespace test;
namespace test
{
extern LoadConfigData *loadConfigDataobj;
LoadConfigData *loadConfigDataobj = new LoadConfigData;
};
int main()
{
loadConfigDataobj->loadConfig();
cout <<loadConfigDataobj->ctrl_params->active_status_code_v<<endl;
cout <<loadConfigDataobj->ctrl_params->suspended_status_code_v<<endl;
cout <<loadConfigDataobj->ctrl_params->erase_status_code_v<<endl;
return 0;
}
bool LoadConfigData::loadConfig()
{
std::string a = "AC";
std::string b = "SP";
std::string c = "ER";
LoadConfigData::ctrl_params = new LoadConfigData::control_params;
sprintf(loadConfigDataobj->ctrl_params->active,"%s",a.c_str());
sprintf(loadConfigDataobj->ctrl_params->suspended,"%s",b.c_str());
sprintf(loadConfigDataobj->ctrl_params->erased,"%s",c.c_str());
return true;
}
输出:
ER
ER
ER
这意味着它正在为每个结构成员打印最后一个复制的字符串。我的代码有什么问题。