我在gsoap客户端应用程序中传递请求变量时遇到问题。错误是Segmentation Fault(核心转储)。
这是我的头文件:
class _ns1__NewTransactionDataRequest;
class _ns1__NewTransactionDataResponse;
class _ns1__GetTransactionResultRequest;
class _ns1__GetTransactionResultResponse;
class _ns1__NewTransactionDataRequest
{
public:
char* UserID 1;
char* UserPwd 1;
char* TransID 1;
char* TransDate 1;
char* A_NO 1;
char* B_NO 1;
char* Denom 1;
char* TransType 1;
struct soap *soap ;
};
class _ns1__NewTransactionDataResponse
{ public:
char* TransID 1;
char* ResultCode 1;
char* ResultDesc 1;
struct soap *soap ;
};
class _ns1__GetTransactionResultRequest
{
public:
char* UserID 1;
char* UserPwd 1;
char* TransID 1;
char* TransDate 1;
char* A_NO 1;
char* B_NO 1;
char* Status 1;
char* Remark 1;
struct soap *soap ;
};
class _ns1__GetTransactionResultResponse
{
public:
char* TransID 1;
char* ResultCode 1;
char* ResultDesc 1;
struct soap *soap ;
};
int __ns1__NewTransactionData(_ns1__NewTransactionDataRequest* ns1__NewTransactionDataRequest, _ns1__NewTransactionDataResponse* ns1__NewTransactionDataResponse
);
int __ns1__GetTransactionResult(_ns1__GetTransactionResultRequest*ns1__GetTransactionResultRequest,_ns1__GetTransactionResultResponse* ns1__GetTransactionResultResponse
);
这是我尝试编译的客户端应用程序:
int main(int argc, char **argv)
{
struct soap soap;
_ns1__GetTransactionResultRequest *ns1__GetTransactionResultRequest;
_ns1__GetTransactionResultResponse *ns1__GetTransactionResultResponse;
_ns1__NewTransactionDataRequest *ns1__NewTransactionDataRequest;
_ns1__NewTransactionDataResponse *ns1__NewTransactionDataResponse;
(*ns1__GetTransactionResultRequest).UserID = "myuserid"; //<--fail at this line
(*ns1__GetTransactionResultRequest).UserPwd = "userpwd";
(*ns1__GetTransactionResultRequest).TransID = "amwani";
(*ns1__GetTransactionResultRequest).TransDate = "2013-09-09 01:01:01";
(*ns1__GetTransactionResultRequest).A_NO = "mynumber";
(*ns1__GetTransactionResultRequest).B_NO = "yournumber";
(*ns1__GetTransactionResultRequest).Status = "success";
(*ns1__GetTransactionResultRequest).Remark = "done";
printf("Content-type: text/html\r\n\r\n<html><h1>Magic Square of Rank</h1><pre>\n");
if (soap_call___ns1__GetTransactionResult(&soap, server, NULL, ns1__GetTransactionResultRequest, ns1__GetTransactionResultResponse))
{
soap_print_fault(&soap, stderr);
soap_print_fault_location(&soap, stderr);
}
else
{
printf("%s", (*ns1__GetTransactionResultRequest).TransID);
}
printf("</pre></html>\n");
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
}
我在compilaton期间没有收到任何错误,但很少有警告:
xpulsaclient.cpp: In function ‘int main(int, char**)’:
xpulsaclient.cpp:46: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:47: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:48: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:49: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:50: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:51: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:52: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:53: warning: deprecated conversion from string constant to ‘char*’
xpulsaclient.cpp:42: warning: unused variable ‘ns1__NewTransactionDataRequest’
xpulsaclient.cpp:43: warning: unused variable ‘ns1__NewTransactionDataResponse’
xpulsaclient.cpp:46: warning: ‘ns1__GetTransactionResultRequest’ is used uninitialized in this function
xpulsaclient.cpp:57: warning: ‘ns1__GetTransactionResultResponse’ may be used uninitialized in this function
也许我在类 ns1 _GetTransactionResultRequest中初始化变量的方式不正确导致了分段错误?谁能帮我?我已经被困在这一点上2天了......谢谢!
答案 0 :(得分:0)
你是对的 - 你没有初始化ns1__GetTransactionResultRequest
(或任何其他指针)。您声明的变量是指针,但它们永远不会分配给新对象。您可以实例化对象,也可以在堆栈中声明变量。
我在另一个回复中发布了一个简单的gSOAP客户端应用程序。它包括对gSOAP请求对象属性的赋值,它可以帮助你。
示例:making a web services query using gSoap with query arguments