In this example, from Robert Lafore's C++ book, no const keyword was used by the author howewer, trying to execute the same code in Visual Studio 2017 gives errors listed in below. I am not sure whether the author has made a mistake here. Ultimately, adding a const keyword has fixed the situation for me.
Here are the errors in case they help: 1- E0415 no suitable constructor exists to convert from "const char [5]" to "String"
2- 'initializing': cannot convert from 'const char [5]' to 'String'
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class String {
/*
.
.
.
*/
String(const char s[]) { //Please note that actually there is no const keyword in the book. I've just put it in there.
strcpy_s(str, s);
}
/*
.
.
.
*/
};
int main() {
String s1 = "hey";
}
Why do I have to use a const and why did the author omit that const(Is this on purpose or was it okay by the time he was writing this book?)?