简体   繁体   English

错误:从'void *'到'test :: apr_size_t * {aka long unsigned int *}'的无效转换'[-fpermissive]

[英]error: invalid conversion from ‘void*’ to ‘test::apr_size_t* {aka long unsigned int*}’ [-fpermissive]

back again with a "using C in C++" kind of question. 再次提出“在C ++中使用C”这类问题。 In my experiment to use APR in C++ I am facing a new issue. 在我的C ++中使用APR的实验中,我面临一个新问题。 The C++ header file: C ++头文件:

#ifndef TEST_STRINGCOMMONS_H_
#define TEST_STRINGCOMMONS_H_

namespace test {
class StringCommons {
public:
    static char* substr_offset_length(apr_pool_t *pool, const char* input,
                                  apr_size_t offset, apr_size_t length);
};
} /* namespace test */
#endif /* TEST_STRINGCOMMONS_H_ */

and the C++ implementation of it: 以及它的C ++实现:

namespace test {
...
char* substr_offset_length(apr_pool_t *pool, const char* input, apr_size_t offset, apr_size_t length)
{
    apr_size_t *input_length = apr_pcalloc(pool, sizeof(apr_size_t));
...
}

} // namespace test

By compiling this class I get the error: 通过编译此类,我得到了错误:

error: invalid conversion from ‘void*’ to ‘test::apr_size_t* {aka long unsigned int*}’ [-fpermissive]

I would like to know what is wrong with this code. 我想知道这段代码有什么问题。 Somebody help me please. 请有人帮我。

Best regards, SK 最好的问候,SK

apr_pcalloc返回一个void *,您可能需要将其static_cast到所需的类型(在这种情况下为apt_size_t *)。

In C++, any pointer can be implicitly converted to void* (just as in C). 在C ++中,任何指针都可以隐式转换为void* (就像在C中一样)。 But unlike C, in C++ a pointer of void* type can't be implicitly converted to int* , or void** , or std::string* , or whatever. 但是与C不同,在C ++中,不能将void*类型的指针隐式转换为int*void**std::string*或其他任何形式。

The solution is reinterpret_cast : 解决方案是reinterpret_cast

apr_size_t *input_length = reinterpret_cast<apr_size_t *>(apr_pcalloc(pool, sizeof(apr_size_t)));

Although why would anybody want to allocate a lonely long on the heap is beyond me. 尽管为什么有人要在堆上分配一个寂寞的long超出了我的范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 g ++编译器:从&#39;pthread_t {aka long unsigned int}&#39;到&#39;void *(*)(void *)&#39;的错误无效转换[-fpermissive] - g++ compiler: error invalid conversion from 'pthread_t {aka long unsigned int}' to 'void* (*)(void*)' [-fpermissive] 错误:从 &#39;void*&#39; 到 &#39;const uint8_t* {aka const unsigned char*}&#39; [-fpermissive] 的无效转换 - error: invalid conversion from 'void*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive] 从&#39;long int&#39;到&#39;void(*)()&#39;的无效转换[-fpermissive] - invalid conversion from 'long int' to 'void (*)()' [-fpermissive] 从“const char*”到“uint32_t {aka unsigned int}”的无效转换[-fpermissive] - invalid conversion from ‘const char*’ to ‘uint32_t {aka unsigned int}’ [-fpermissive] 错误:从&#39;int&#39;到&#39;void *&#39;的无效转换[-fpermissive] - error: invalid conversion from 'int' to 'void*' [-fpermissive] 编译器错误:无效转换从int *到unsigned int * [-fpermissive] - Compiler Error: Invalid Conversion from int* to unsigned int* [-fpermissive] 从&#39;const void *&#39;到&#39;PVOID {aka void *}&#39;的无效转换[-fpermissive] - invalid conversion from 'const void*' to 'PVOID {aka void*}' [-fpermissive] 从“const void*”到“PVOID {aka void*}”[-fpermissive] 的无效转换 - invalid conversion from ‘const void*’ to ‘PVOID {aka void*}’[-fpermissive] 从std :: size_t *到long unsigned int的转换无效* - Invalid conversion from std::size_t* to long unsigned int* 错误:在给定的命令集中从 'int' 到 'void*' [-fpermissive] 的无效转换 - error: invalid conversion from ‘int’ to ‘void*’ [-fpermissive] in the given set of commands
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM