[英]C++ how would I cast a string to a pointer?
I'm playing with this C wrapper for the OpenCV function imread. 我正在使用此C包装程序来读取OpenCV函数。
Mat* cv_imread(String* filename, int flags) {
return new Mat(cv::imread(*filename, flags));
}
It's failing somewhere I think so I'm trying to test to make sure it's written right. 我认为某处出现故障,因此我尝试进行测试以确保其编写正确。 I just need to know how to create a
String*
to fill its first argument.. I've tried creating a string with 我只需要知道如何创建
String*
来填充其第一个参数即可。
char* filename = "~/home/test.jpg"
then casting to String*
i/e 然后转换为
String*
i / e
(String*)filename
but that's not working..I've tried many other variations to cast to pointer found online but found nothing cv_imread
would accept. 但这是行不通的。我尝试了许多其他变体以转换为在线找到的指针,但没有发现
cv_imread
可以接受。 In my code it's necessary to have the filename
parameter be a String*
and not another type. 在我的代码中,必须使
filename
参数为String*
而不是其他类型。 But I could use help creating a String*
to give to cv_imread
. 但是我可以使用帮助创建一个
String*
给予cv_imread
。
Edit: per your edit I tried 编辑:根据您尝试的编辑
const char* filename = "/home/w/d1";
cv_imread(new string (filename), 1);
but got error: 但出现错误:
cannot convert 'std::string* {aka std::basic_string }' to 'cv::String ' for argument '1' to 'cv::Mat* cv_imread(cv::String*, int)' cv_imread(new string (filename), 1); 无法将参数'1'的'std :: string * {aka std :: basic_string }'转换为'cv :: String '到'cv :: Mat * cv_imread(cv :: String *,int)'cv_imread(新字符串(文件名),1);
If you can help me with it I'd appreciate it 如果您能帮助我,我将不胜感激
Use string::c_str()
to get the underlying pointer of a string: 使用
string::c_str()
获取字符串的基础指针:
Mat* cv_imread(String* filename, int flags) {
return new Mat(cv::imread(filename->c_str(), flags));
}
Or you can use string::data()
too. 或者您也可以使用
string::data()
。 They have the same functionality after C++11. 在C ++ 11之后,它们具有相同的功能。
Edit: OP seems to want to pass a char*
to cv_read
. 编辑:OP似乎想将
char*
传递给cv_read
。 Then should do this: 然后应该这样做:
cv_read(new String(filename), 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.