简体   繁体   中英

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. In my experiment to use APR in C++ I am facing a new issue. The C++ header file:

#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:

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

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

In C++, any pointer can be implicitly converted to void* (just as in 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.

The solution is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM