Ask Your Question

imread and CString

asked 2017-05-25 21:36:09 -0600

djtommye gravatar image

Okay - I feel really stupid asking this question. But I've fought for this for two days now...

I'm writing in Visual Studio, C++. I'm passing an LPSTR to a function and, in that function, referencing the LPSTR to open a file. But it fails. If I hard-code a value, it succeeds. I'm getting all kinds of typecast warnings. I coded for 12 years but have been out of the business for 16 - so I'm getting back up to speed. Any help would be greatly appreciated!

BOOL CImageDialog::DisplayImage(LPCTSTR pszImageFilePath)
    Mat im = imread(pszImageFilePath);

    if (! {
        return FALSE;


no suitable constructor exists to convert from "LPCTSTR" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"
cv::Mat cv::imread(const std::string &,int)': cannot convert argument 1 from 'LPCTSTR' to 'const std::string &'

I've tried passing other types, but they all cause issues. Thank you in advance!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-05-25 22:50:16 -0600

black9 gravatar image

updated 2017-05-25 23:10:57 -0600

 string PointToString(LPCTSTR filepath){               //function to convert LPCTSRT to string
std::stringstream ss;
ss << filepath;
return ss.str(); }

//your function

 BOOL DisplayImage(LPCTSTR pszImageFilePath){
string imagepath = PointToString(pszImageFilePath);   //call function PointToString
Mat im = imread(imagepath);
if (! {
    return FALSE;
edit flag offensive delete link more


you can't use std::stringstream << CString

pklab gravatar imagepklab ( 2017-05-26 13:33:54 -0600 )edit

answered 2017-05-26 13:27:09 -0600

pklab gravatar image

You need to convert CString that is a wide string to std::string that is 8bit string. This involves locales and so on.

If you are sure that the CString contains only 8bit chars you might use the CW2A macro from MS and write

Mat im = imread(CW2A(pszImageFilePath));

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-05-25 21:36:09 -0600

Seen: 2,824 times

Last updated: May 26 '17