2013-06-26 18 views
0

接收到信號我有一個指針,當我取消對它的引用,它給我的錯誤。問題是參數* ns__personRequestResponse段錯誤的gSOAP的,下停止,因爲它從操作系統

int __ns3__PersonRequest(soap *, _ns1__PersonRequest *ns1__PersonRequest, _ns1__PersonRequestResponse *ns1__PersonRequestResponse) 
{ 
    ns1__PersonRequestResponse->result = 0; 
    //ns1__PersonRequestResponse = new _ns1__PersonRequestResponse(); 
    *ns1__PersonRequestResponse->result = 39; // Error here 
    return SOAP_OK; 
} 

下面是從具有響應參數的wsdl創建的頭文件的一部分。

class _ns1__PersonRequestResponse 
{ 
    public: 
    /// Element result of type xs:int. 
    int*         result       0; ///< Nullable pointer. 
    /// A handle to the soap struct that manages this instance (automatically set) 
    struct soap       *soap       ; 
}; 

我得到段故障時,我將值分配給整數變量的結果。我如何讓它工作?

回答

0

在你的結構的結果字段是一個指向int。在代碼中,你 首先將其初始化爲0,然後試圖通過該指針賦值。 但在大多數系統中,這將失敗,因爲在地址0的內存沒有被 分配給你的程序。

此修復程序旨在確保result在嘗試 之前指向通過該指針的有效內存。究竟應當如何發生取決於 該結構將如何在你的代碼中使用。一種方法是 聲明你的函數的變量int外(可能在文件範圍內), 再取它的地址,並將其分配給result

int my_result; // This should be declared OUTSIDE of a function! 

int __ns3__PersonRequest(soap *, _ns1__PersonRequest *ns1__PersonRequest, _ns1__PersonRequestResponse *ns1__PersonRequestResponse) 
{ 
    ns1__PersonRequestResponse->result = &my_result; // Make sure pointer points to valid memory 
    *ns1__PersonRequestResponse->result = 39; // Updates the value of my_result 
    return SOAP_OK; 
} 
+0

它詳細介紹了這個錯誤。錯誤:從「詮釋」到「詮釋*」的無效轉換[-fpermissive] – Kahn

+0

@Hesper:你肯定在結構中的「結果」字段真是聲明爲一個整數?也許你在結構聲明中有一個不必要的'*'....你應該編輯你的問題來包含聲明。 –

+0

我現在已經包含了具有ns1__PersonRequestResponse定義的頭文件的一部分。 star是必須的,因爲它的gsoap返回類型,它應該是一個指針。 – Kahn

相關問題