0

I have a project that uses MFC and standard C++. I dont know in which VS it was originally made. When opened on the machine with only VS2012 installed and on the machine with only VS2010 installed - it doesn't build, showing errors, mostly regarding type mismatch. For example:

CListBox &ltbx
CString s;
ltbx.AddString(s);

This shows error, saying that AddString doesn't take argument of a type CString. The same goes for

CListBox &ltbx
std::string str;
ltbx.AddString(str.c_str());

This code as well shows an error:

string str;
CString CInputID;
str = static_cast<string> (CInputID);

It says that there is no user defined conversion from CString to std::string.

No doubt, I should be using different types for strings and all, but the exact same project opened on the machine which has installed both VS2010 and VS2008 works just fine, all types are matched. So my question is how does this work? I am thinking, maybe VS2008 has some libraries that define all necessary conversions and VS2010 is just making use of them. If so, what are those libraries and how can i make this project work on machines with no 2008 studio installed?

Stranger1399
  • 81
  • 2
  • 12
  • There is an assignment to that `ltbx` reference? You can't declare a reference without making it reference something. – Some programmer dude Feb 02 '14 at 11:28
  • when you right-click on `CListBox` and select "go to declaration", where do you get? – wimh Feb 02 '14 at 11:31
  • 2
    Also, `CString != std::string` You can't cast one type as the other. – Some programmer dude Feb 02 '14 at 11:33
  • You may want to research what [`static_cast`](http://en.wikipedia.org/wiki/Static_cast) does, because it isn't that. – WhozCraig Feb 02 '14 at 11:37
  • @Joachim Pileborg Yes there is an assignment to that ltbx reference. Its a parameter of a function, I just put it to indicate the type of ltbx. @Wimmel CListBox and select "go to declaration" that leads to a file afxwin.h, code: `class CListBox : public CWnd{...};` The point of question is not about matching the types, its about how the types are matched in a certain configuration (both VS2008 and VS2010 installed). The project build on a certain machine and works fine – Stranger1399 Feb 02 '14 at 11:52

1 Answers1

1

AddString takes a LPCTSTR which is a const char* or const wchar_t* depending on whether _UNICODE is defined.

The difference is probably that _UNICODE is defined in your VS2012 project.

parkydr
  • 7,596
  • 3
  • 32
  • 42
  • That is exactly what was the problem. Thanks a lot! – Stranger1399 Feb 02 '14 at 12:22
  • It's probably worth noting that in VS2013 and on, using Multi-Byte Character Set in MFC is deprecated, so rather than undefining `_UNICODE`, you should look at changing your code. If you need a `std::basic_string` from a `CString`, you can use the `CT2A` macro as [described here](http://stackoverflow.com/a/859841/218597). – icabod Feb 17 '14 at 11:29