2017-01-24 25 views
0

Cで書かれたコールバックをC++に適応させようとしているときに、C言語に問題があります。コンパイラは次のエラーを示していますC関数のC++への変換

error C2664: 'fread' : cannot convert parameter 4 from 'void *' to 'FILE*' 
1>Conversion from 'void*' to pointer to non-'void' requires an explicit cast 

私はこのsize_t retcode = fread(ptr, size, nmemb, stream)castが必要であることがわかりました。私は試しましたが、何も動作しません。ここでは、コードは次のとおりです。

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) 
{ 
    curl_off_t nread; 
    size_t retcode = fread(ptr, size, nmemb, stream); 
    nread = (curl_off_t)retcode; 
    fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T 
      " bytes from file\n", nread); 
    return retcode; 
} 
+0

キャストすることはできますが、パラメータタイプをFILE *に変更する方が簡単ではないでしょうか?あなたが提供していない残りの構造に依存します。 – DrC

+5

__エラーメッセージを注意深くお読みください。これを修正するために何ができるか教えてください。 – ForceBru

+1

http://stackoverflow.com/questions/8523318/void-to-file-is-that-posable – samnaction

答えて

2
fread(ptr, size, nmemb, static_cast<FILE*>(stream)) 
1

これは、有効なC++ではありません完全に有効なCコードの例です。 void*から任意のポインタ型(および後ろ)への暗黙の型変換は、C標準によって明示的に許可されています。

§6.3.2.3 ¶ 2

A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

しかし、C++は唯一void*の方向にそれを可能にする:

§4.11 ¶ 2

A prvalue of type “pointer to cv T”, where T is an object type, can be converted to a prvalue of type “pointer to cv void”. The pointer value ([basic.compound]) is unchanged by this conversion.

他の方向にのみキャストで可能です。

関連する問題