注:API使用的char *,而不是为const char *,所以我不能使用c_str()也函数调用sendCall是Aysnchronous呼叫字符串为char *转换为非静态字符串失败
下面是我的代码,它当我使用
char* payloadString = >&currString[0];
但是当我尝试使用
char* payloadString = &secondCopy[0];
它失败,我不能够能明白其中的道理正常工作。我想在下面的代码中创建一个像desiredString这样的动态可更新字符串,该代码将版本变量并将其分配给静态字符串secondCopy,并且能够使用该字符串代替currString,但我想我通过使用修改操作符的地址静态字符串。请提出解决方法。
void functionName()
{
std::string version;
version = "ABC";
static std::string currString= "<Version=\"3.0\" Ret=\"false\"/>";
std::string desiredstring= "<Version="+version+" Ret=\"false\"/>";
static std::string secondCopy = desiredstring;
char* payloadString = &currString[0];
//char* payloadString = &secondCopy[0];
XDSC::Definition aDefinition("sname", "sid");
try
{
std::auto_ptr<otf::ClientSession> aSession;
aSession = getResources()->getManager().getSession("XML");
aSession->setAttachedToServerConversation(true);
aSession->setLogicalName("xyz");
aSession->setITOReturnPackage("svrc");
boost::shared_ptr<Payload> aPayload = boost::make_shared<Payload>(aDefinition, "3.0",payloadString, strlen(payloadString));
sendCall(aSession.get(), aPayload,"SRING", true);
}
catch(std::exception& e)
{
throw (exception(ERROR,"SendCall Failed to Send"));
}
}
为什么不直接使用'secondCopy.c_str()'? API是否只接受'char *'而不是'const char *'? – crashmstr
我试过使用c_str(),但它返回一个const char *,下面的函数需要一个char *而不是一个const char * –
不知道这是一个问题还是一个旁注,但是你的两个版本字符串(除了'3.0'和'ABC')不一样。 'currString'在值周围有引号,'desiredString'没有。 – crashmstr