0

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?)?

  • 5
    Please get yourself a better [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you are using the one I found on amazon it was published in 1996 before C++ was even standardized and way before "modern" C++. – NathanOliver Apr 10 '18 at 21:13
  • My book was published in around 2006-2009. – Çağan Kuyucu Apr 10 '18 at 21:15
  • also "gives some error" is not very helpful thing to report – pm100 Apr 10 '18 at 21:15
  • What is the title of the book? – NathanOliver Apr 10 '18 at 21:15
  • Object Oriented Programming in C++ (4th Edition). – Çağan Kuyucu Apr 10 '18 at 21:19
  • I assume you are using [MS `strcpy_s()`](https://msdn.microsoft.com/de-de/library/td1esda9.aspx)? While there is one in the optional and detested annex k of the C standard which has one more argument, there is none in the C++ standard. Also, be aware that [`using namespace std;` is beyond bad form](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), and may only potentially be justifiable by the author having to cater to the editor on size to get published at all. – Deduplicator Apr 10 '18 at 21:19
  • I'd still recommend one of the books from the link in my first comment. It looks like yours was published in 2001. This book does not look like it will teach you standard C++ and is more of a C with classes type book (which is not what C++ is) – NathanOliver Apr 10 '18 at 21:23
  • @NathanOliver Thanks for the advice I'll consider getting a new one. – Çağan Kuyucu Apr 10 '18 at 21:36

2 Answers2

2

That because the "hey" you're passing into your constructor is a const char * and you can't pass a const value to a function declared to take a non-const parameter.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
2

When you initialize a string with the following line, you actually provide it a const char *.

String s1 = "hey";

However, unless you have a constructor that takes const char * as parameter, you basically have no constructor with which to build an object. Because, a const char * can not be automatically cast to char *. If that could be done, there would be no point in having a const keyword at all.

So, if you must have a String constructor that takes as parameter a char *, then you should consider copying the characters in "hey" to a char array A and then pass A into the constructor.

Alternatively, just keep the const keyword.

ilim
  • 4,477
  • 7
  • 27
  • 46