5

I'm a long time Java/C# developer, and I'm trying to teach myself C++, specifically MFC, using a book by Richard Jones that was published in 2000.

I'm getting this compile error:

'ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::Format': no overloaded function could convert all the argument types

On the following code that I typed in from an example in the book:

int r;
double rd;
int i;
CString s;

std::cout << "\nLargest random integer: " << RAND_MAX << std::endl;

//Provide seed for generator
srand((unsigned)time(NULL));

std::cout << "\n5 random integers: \n";
for (i = 0; i < 5; i++) {
    r = rand();
    s.Format("%d ", r);  
    std::cout << s << std::endl;
}

I'm using Visual Studio 2022, and I'm wondering if I need to configure it differently to use examples from over 20 years ago?

Perhaps related to my confusion, I don't even see a CString class listed here: Walkthrough: Using the New MFC Shell Controls - only CStringArray and CStringList.

As directed in my book, I'm trying to use these MFC classes in a console program as a starting point. I have my C++ Language Standard set to "Default (ISO C++14 Standard)" in the project properties. I'm wondering if it would help to set it to whatever the standard was around the year 2000?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
stevoh
  • 53
  • 3
  • 4
    `CString` -> `CStringA` I suspect, with the code as written. Further reading: https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/using-cstring?view=msvc-170 – Paul Sanders Jul 25 '23 at 23:25
  • 1
    I have a feeling that's the `wchar_t` getting you. Give `s.Format(L"%d ", r);` a try to see if feeding a wide character format argument makes the compiler happy. Whole tons of stuff have changed over the past couple decades. For example, Microsoft typically expects wide characters to be pretty much defacto for any user-facing string and you have to either go with it (usually for the best) or explicitly tell it not to. – user4581301 Jul 25 '23 at 23:25
  • 1
    @user4581301 Then the following line will barf. All ANSI or all Unicode is the way to go, except where a conversion is unavoidable. – Paul Sanders Jul 25 '23 at 23:27
  • Good point. Go all wide or all ANSI. I recommend going all wide for any serious programs going to customers. – user4581301 Jul 25 '23 at 23:29
  • 7
    Don't use a 20 year old book to learn C++. Too much has changed. Especially C++11 was a rather big revolution (evolution really, but it feels bigger) and C++17 and C++20 also added heaps of new and important stuff. A 20 year old book will teach you seriously obsolete C++ - the old stuff still works, sure, we just have so many better ways now that the old stuff is almost considered bad practice these days. – Jesper Juhl Jul 25 '23 at 23:30
  • 1
    @user4581301 _go all wide for any serious programs going to customers_ Absolutely. – Paul Sanders Jul 25 '23 at 23:32
  • To bring you up to speed, try one of these: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Paul Sanders Jul 25 '23 at 23:33
  • 5
    You will do yourself a huge favor by completely forgetting everything about the non-standard `CString` and other key players of Microsoft embrace-and-extend campaigns, and focus on learning standard C++ classes, like `std::string` from a recent C++ textbook. – Sam Varshavchik Jul 25 '23 at 23:37
  • 3
    "*I don't even see a `CString` class listed here ...*" - of course not, because string classes are not Shell controls. See [Using CString](https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/using-cstring?view=msvc-170) instead. – Remy Lebeau Jul 25 '23 at 23:51
  • Wow, so much great info here. Thank you! Based on some of the responses, I can see that I didn’t make something very clear. Before this 20 year old MFC book, I read the current edition of “A Tour of C++” and loved it. In fact, it made me realize that (today’s) C++ is a fabulous, modern language. My only prior experience (with C or C++) was with C, over 20 years ago. So, the thing I didn’t make clear is that I’m trying to learn how to write desktop GUI apps in C++ that don’t need .Net. I’m assuming MFC is still relevant. If anyone can recommend an MFC book, I would be very grateful. – stevoh Jul 26 '23 at 14:55
  • 1
    Side note: Even 20 years ago C++ and C were pretty different, but massive numbers of students, myself included, were horribly underserved by being taught C programming and very little C++ programming in C++ courses. This continues to this day. – user4581301 Jul 26 '23 at 17:24
  • On the off chance that some other budding C++ GUI developer finds themselves here, my advice would be, don't bother with MFC initially. One of the responses to my question, lead me to theForger's Win32 API Tutorial (http://winprog.org/tutorial/), and I'm discovering you can write basic GUI's w/ just this. – stevoh Jul 27 '23 at 00:37
  • Your `s.Format("%d ", r);` line should be `s.Format(_T("%d "), r);` or `s.Format(L"%d ", r);` . I can explain you more after you try it. – sergiol Aug 14 '23 at 09:42

1 Answers1

8

By default, the character set in visual studio is set to Unicode which is why you're getting the error.

To resolve the issue, follow the below steps:

  1. Go to Project --> [your_Project_Name] Properties:

enter image description here

  1. Under Configuration Properties, select "Advanced":

enter image description here

  1. Finally, on the right window, click on "character set" and select "not set":

enter image description here

Click ok, and your code should compile.

Required include files:

#include <atlstr.h>
#include <iostream>       
#include <ctime>
machine_1
  • 4,266
  • 2
  • 21
  • 42
  • You nailed it. Thank you. – stevoh Jul 26 '23 at 14:56
  • 1
    Making a problem go away and solving a problem are not necessarily the same thing. This proposed solution falls into the *"making the problem go away"* bucket and is literally as bad as what the OP started out with. – IInspectable Aug 06 '23 at 13:25