0

I started learning C++ on a book a while ago and now I'm stuck with a part of code from the book that doesn't work on my API, which is Visual Studio 2019. The book is from 2000, so this might be part of the problem, but if it is could you tell my how to patch it?

The problem is in the following code. The author of the book wants to use a char array as an argument for the constructor and does it with a pointer (char* pName). However, Visual Studio underlines the argument ("0. DannyBoy"). I looked around for answers, but none look like mine. It would be much appreciated if someone could help me!

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

const int MAXNAMESIZE = 40;

class Student
{
public:
    Student(char* pName)
    {
        strncpy_s(name, pName, MAXNAMESIZE);
        name[MAXNAMESIZE - 1] = '\0';
        semesterHours = 0;
        gpa = 0;
    }

    //... autres membres publics...
protected:
    char name[MAXNAMESIZE];
    int semesterHours;
    float gpa;
};

int main(int argcs, char* pArgs[])
{
    Student s("0. DannyBoy");
    Student* pS = new Student("E. Z. Rider");

    system("pause");
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Use the qualifier const in the parameter declaration Student(const char* pName) String literals in C++ have types of constant character arrays. – Vlad from Moscow Jun 17 '20 at 15:40
  • What book are you using? That wouldn't work in 2000 either. May I recommend a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Eljay Jun 17 '20 at 15:43
  • *I started learning C++ on a book* -- Do not learn from 20+ year old books. Did the book also recommend to use `new` when not necessary? Like here:`Student* pS = new Student("E. Z. Rider");`? This should be `Student pS("E. Z. Rider");` – PaulMcKenzie Jun 17 '20 at 15:51

3 Answers3

1

String literals are of type const char [] which decay to const char *. Your constructor should take a const char *:

//      VVVVV
Student(const char* pName)
{
    strncpy_s(name, pName, MAXNAMESIZE);
    name[MAXNAMESIZE - 1] = '\0';
    semesterHours = 0;
    gpa = 0;
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

On this line:

Student s("0. DannyBoy");

you are passing a string literal, which is of type char[12] to the constructor of Student.

However, you need to use a char const * to bind to a char array, so your constructor needs to look like this:

Student(char const * pName) {
cigien
  • 57,834
  • 11
  • 73
  • 112
0

In C historically string literals have types of non-constant character arrays. And before the C++ 11 Standard C++ compilers allowed to use string literals as arguments of parameters having non constant type char * for backward compatibility.

However though in C string literals have non-constant character arrays nevertheless you may not change them.

In C++ 11 there was decided not to allow to use string literals with the type char * because in C++ they have types of constant character arrays.

So declare the constructor like

Student( const char *pName )

In any case it is better because this declaration says the reader of the class that the passed string will not be changed in the constructor even if the argument will not be a string literal.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335