2017-02-10 51 views
-1

我想寫一個函數,它可以返回「malloc」函數指針。返回函數指針錯誤沒有typedef?

但有一個錯誤,該平臺是Windows 7 +的Visual Studio 2015年

源代碼 「malloc.cpp」:

#include <cstdlib> 


namespace portable { 

    static void *(*malloc_impl)(size_t) = std::malloc; 
    static void (*free_impl)(void*) = std::free; 
    static void *(*realloc_impl)(void*, size_t) = std::realloc; 
    static void *(*calloc_impl)(size_t, size_t) = std::calloc; 

#if defined(_WIN32) && defined(__MINGW32__) 
# error("sorry, we don't support MinGW32 compiler with Windows") 
#endif 

#if defined(_WIN32) && defined(_MSC_VER) 
# include <WinSock2.h> 
# include <Windows.h> 
    static size_t (*malloc_usable_size_impl)(void*) = _msize; 
#elif defined(__APPLE__) 
# include <malloc.h> 
    static size_t (*malloc_usable_size_impl)(void*) = malloc_size; 
#else 
# include <malloc.h> 
    static size_t (*malloc_usable_size_impl)(void*) = malloc_usable_size; 
#endif 

    void *(*o_set_malloc(void *(*func)(size_t))) (size_t) throw() 
    { 
     void *(*old)(size_t) = malloc_impl; 
     if (func) { 
      malloc_impl = func; 
     } 
     return old; 
    } 
    size_t (*o_set_malloc_usable_size(size_t (*func)(void*))) (void*) throw() 
    { 
     size_t (*old)(void*) = malloc_usable_size_impl; 
     if (func) { 
      malloc_usable_size_impl = func; 
     } 
     return old; 
    } 
    void (*o_set_free(void (*func)(void*))) (void*) throw() 
    { 
     void (*old)(void*) = free_impl; 
     if (func) { 
      free_impl = func; 
     } 
     return old; 
    } 
    void *(*o_set_realloc(void *(*func)(void*, size_t))) (void*, size_t) throw() 
    { 
     void *(*old)(void*, size_t) = realloc_impl; 
     if (func) { 
      realloc_impl = func; 
     } 
     return old; 
    } 
    void *(*o_set_calloc(void *(*func)(size_t, size_t))) (size_t, size_t) throw() 
    { 
     void *(*old)(size_t, size_t) = calloc_impl; 
     if (func) { 
      calloc_impl = func; 
     } 
     return old; 
    } 
} 

編譯器報錯:

嚴重性代碼說明項目文件行抑制狀態 錯誤(活動)

不兼容異常規格

d:\ work \ project \ orange \ src \ portable \ malloc.cpp 56

看起來「老」與返回類型不兼容。請幫幫我。

+0

請回答這個問題。 – linrongbin

+1

[mcve]會相當有幫助。我無法在http://webcompiler.cloudapp.net/上重現任何編譯器錯誤。 – chris

回答

-1

感謝評論,我找到了問題。

在Windows 7 + VS2015中,我定義是這樣的:

#define NOEXCEPT throw() 

不相容爲 「void *(*)(size_t)」 和 「void *(*)(size_t) throw()」 之間。