0

My code is this:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;
const int LENGTH1 = 25;

char array_str1[LENGTH1] = "Hey guys! "; 
char array_str2[] = "What's up?"; 

string std_str1 = "Hello everyone! ";
string std_str2 = "How's tricks?";

int main(){ 
    strcat_s(array_str1, LENGTH1, array_str2);
    std_str1 += std_str2;
    cout << std_str1 << endl;
    return 0;
}

When I run it, I get the error:

use of undeclared identifier 'strcat_s'

If I comment out the strcat_s line, the rest of the code runs fine.

I'm working from a tutorial, and even if I copy-paste the working code from the tutorial, I get the same error. I'm using Visual Studio Code 1.60.0 on MacOS 10.14.16.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
kubleeka
  • 11
  • 3
  • 2
    [`strcat_s`](https://en.cppreference.com/w/c/string/byte/strcat) is a C function, it is not provided by C++. `` is a C++ header. – François Andrieux May 10 '22 at 18:24
  • 2
    And why does C++ not provide such a useful-sounding function? Because it's not all that useful. Use `std::string`. – user4581301 May 10 '22 at 18:29
  • 1
    Visual Studio Code is not a compiler. Which compiler are you using with it? Some compilers provide `strcat_s`, some don't – Remy Lebeau May 10 '22 at 18:32
  • My crystal ball tells me that "working code" from the tutorial is from an author utilizing the Visual C++ toolchain, which *does* include `strcat_s` (and all the other `insertnamehere_s` functions) as part of their implementation, including their C++ runtime. But it is *not* standard C++. MS even goes as far as providing a template version deducing the non-type template length parameter in the event you're using declared arrays (like you are); also utterly not part of the standard C++ library. – WhozCraig May 10 '22 at 18:32
  • More specifically, `strcat_s` was introduced to the C language in C11. C++ does not promise to add functions that were added to another language. – Drew Dormann May 10 '22 at 18:32
  • `#include ` -- A C++ looking at your code will quickly point out that you are including ``. So why are you using `strcat_s` and not `std::string`? – PaulMcKenzie May 10 '22 at 18:32
  • @DrewDormann -- I just realized that. Amended my comment. – PaulMcKenzie May 10 '22 at 18:34
  • 1
    *If I comment out the strcat_s line, the rest of the code runs fine.* -- And if you leave it commented out and instead used `std::string` throughout your code, you would be better off. – PaulMcKenzie May 10 '22 at 18:34

1 Answers1

2

Is <cstring> not including the strcat function?

<cstring> does declare std::strcat.

Sidenote: Don't use std::strcat; you won't need it. Use std::string.

strcat_s(array_str1, LENGTH1, array_str2);

strcat_s is not std::strcat. Even though there is std::strcat in the C++ standard library, there is no strcat_s.

Sidenote: There is strcat_s in the C standard library (since C11). C is a different language. Even in C, strcat_s is not guaranteed to be supported, and not all language implementations provide it.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • All of which only goes to demonstrate, lest we forget, that C++ is no longer a 'superset' of C. – Paul Sanders May 10 '22 at 20:55
  • ... So if you're going to code in C++, you should forget everything you know about C and [do it properly](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Paul Sanders May 10 '22 at 21:02