[英]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.