再次回到“使用C ++中的C”这样的问题。在我在C ++中使用APR的实验中,我遇到了一个新问题。 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_ */
及其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
通过编译这个类我得到错误:
error: invalid conversion from ‘void*’ to ‘test::apr_size_t* {aka long unsigned int*}’ [-fpermissive]
我想知道这段代码有什么问题。有人帮帮我。
祝你好运, SK
答案 0 :(得分:1)
apr_pcalloc返回一个void *,你可能需要static_cast到你想要的类型(在这种情况下是apt_size_t *)。
答案 1 :(得分:0)
在C ++中,任何指针都可以隐式转换为void*
(就像在C中一样)。但与C不同,在C ++中,void*
类型的指针不能隐式转换为int*
,void**
或std::string*
,或其他任何内容。
解决方案是reinterpret_cast
:
apr_size_t *input_length = reinterpret_cast<apr_size_t *>(apr_pcalloc(pool, sizeof(apr_size_t)));
虽然为什么有人想在堆上分配一个孤独的long
超出我的范围。