2017-04-25 110 views
0

哪些錯誤與下面的代碼,即使我已經正確使用變量file_op非靜態變量,也宣告正確我得到下面的錯誤....獲得invalig使用C++非靜態數據成員的錯誤

class ILV_file_operations: public ILV_command 
{ 
private: 
    /** 
    * \copydoc ILV_command::ILV_command_request 
    */ 
    class Request: public ILV_command_request<ILV_file_operations> 
    { 
     friend class ILV_file_operations; 
     // New format parameters 
     uint32_t file_op; 
     std::string file_path; 
     std::string file_read_data; 
     uint8_t request_status; //!< Response status 
    public: 
     explicit Request(ILV_file_operations& parent_ref); 
     ~Request(); 

     /** 
     * \brief This is method to read the parameters received in the request. 
     * \return 
     * \arg \c Length: Length of the request data read 
     */ 
     uint32_t read_params(); 
    }; 

    class Response: public ILV_command_response<ILV_file_operations> 
    { 
     friend class ILV_file_operations; 
     //New parameter 
     std::list<Sub_ILV> param_ILV; ///< Parameter ILV 

     // Old format parameters 
     std::string file_write_data; ///< Serial number 
     uint8_t response_status; //!< Response status 
    public: 
     explicit Response(ILV_file_operations& parent_ref); 
     ~Response(); 

     /** 
     * \brief This is method to calculate the length of the response to be sent. 
     * \return 
     * \arg \c Length: Length of the response data 
     */ 
     uint32_t calculate_length(); 
     /** 
     * \brief This is method to write the parameters in the response. 
     * \return 
     * \arg \c Length: Length of the response data written 
     */ 
     uint32_t write_params(); 
    }; 

    Response resp_data; ///< Response data object 
    Request req_data; ///< Request data object 

public: 

    /** 
    * \copydoc ILV_command::ILV_command 
    */ 
    explicit ILV_file_operations(boost::shared_ptr<Protocol>& prtcl); 
    /** 
    */ 
    virtual ~ILV_file_operations(); 

    void process(); 
    uint32_t read_packet(); 
    uint32_t write_packet(); 
}; 


uint32_t ILV_file_operations::Response::write_params() 
{ 
    uint32_t ret_val = 0; 

    if (((int)req_data.file_op) == ((int)file_operation_type::write)) 
    { 
     ret_val += parent.cmd_prot->write_string(file_write_data); 
    } 
    return ret_val; 
} 

錯誤:

| Compiling core_app/src-libs/libCommand_manager/src/ILV_commands/ILV_file_operations.cpp ... 
| In file included from core_app/src-libs/libCommand_manager/src/ILV_commands/ILV_file_operations.cpp:11:0: 
| ../include/ILV_commands/ILV_file_operations.h: In member function 'virtual uint32_t Dist_cmd_processor::ILV_file_operations::Response::write_params()': 
| ../include/ILV_commands/ILV_file_operations.h:82:13: error: invalid use of non-static data member 'Dist_cmd_processor::ILV_file_operations::req_data' 
|  Request req_data; ///< Request data object 
|   ^
| /home/sagar/morpho/MASigma_Firmware_REV_1.0.10/src-ma5g/core_app/src-libs/libCommand_manager/src/ILV_commands/ILV_file_operations.cpp:84:15: error: from this location 
|  if (((int)req_data.file_op) == ((int)file_operation_type::write)) 
|    ^

回答

2

你的方法write_params是類ILV_file_operations::Response的一員,但它試圖訪問類ILV_file_operations的非靜態成員變量沒有對象。
首先,您需要類ILV_file_operations的對象來訪問其成員,或者該成員必須是static
但是,你仍然可以不訪問成員變量,因爲它們是private,並且你的內部類不能自動訪問它們外部類的成員變量。