Why does the CPP program written on VS initialize a character array parameter to store a string in the parameter summary table of the constructor, and the compilation shows that the default argument does not match the parameter?

1. Problem description:
in the constructor of the student class, initializing: char sname [] = "no name" to the formal parameter character array will report an error. The error message is: error C2440: "default parameter": cannot be converted from "const char [8]" to "char []"
when instantiating an object with a defined class, the error: student ("Zhu Ming", 86302535) will also be reported at the passed parameter. The error message is: error C2440: "< function-style-cast >": cannot convert from "initializer list" to "student"
2. The general code of the program is as follows

-sharpinclude<iostream>
-sharpinclude<cstring>
-sharppragma warning(disable:4996) //strcpy 
using namespace std;
class studentID {
    long value; //
public:
    studentID(long id = 0) { //id
        value = id;   //
        cout << ":" << value << endl;
    }
    ~studentID() {  //
        cout << ":" << value;
    }
};
class student {
private:
    studentID id; //
    char name[20]; //
public:
    student(char sname[]="no name", long sid = 0) :id(sid) { //                                            
        cout << ":" << sname << endl;
        strcpy(name,sname);
        //name = sname;
    }
    ~student() {
        cout << ":" << name << endl;
    }
};
int main() {
    student("",86302535); //student
    system("pause");
    return 0;
}

main program picture:

:

add: the original purpose of the program is to pass in a person"s name, so an array is used as a formal parameter, and the initialization value is the string "no name", but the above error occurs; I have tried, if you simply modify the program, the input is a single character, then the program is normal. For example, if the initialization of the constructor is modified as follows: char sname="a", of course, the previous data members should also be modified to char name, and other places should also be changed to: strcpy (name,sname) to change the instantiation of name= sname, to student ("Zhu Ming", 86302535) to student ("xmom pencils 86302535), then the program is normal

.
Mar.09,2021

cPP version of pill. I was asked to answer all day. Sure enough, the people stayed. The answer went unanswered.

it has already prompted you. The type of your string "Zhu Ming" is const char* . What is this type?

because cPP's from the beginning of cPP11 :

  • const char* str = "Zhu Ming";
  • const str points to text segment , const protects this data `

or

The purpose of these people is to ensure that the data in text segment (this is string literal ) " Zhu Ming "), so for the security period, cPP11 < / strong

of course, you may have such a thing or:

The type of
obvious shape ( parameter ) is char [] not char* ah.
As a matter of fact, this c is not a first-class citizen in cPP , or derived type , so you can't send a value. No, it's not like that. struct is the same as derived type , it can deliver value. derived type is only composite type alias , and first-class
are irrelevant.

in short, it can't send messages. In other words, it can't do deep copying when sending. All it can do is send instructions. By the same token, if you want to send functions, you can only send them on c . cPP11 starts to use struct to model lambda to realize the function of truly sending functions.

OK, after going around and answering the question, now you should understand that your form here char [] has to be reduced to char* . In fact, the vs hint also hints at you this message. There are so many ways to interpret your words:

  1. use only std::string / std::wstring
  2. shape : change to const char* / char const *

in fact, people discovered a long time ago that text segment is very dangerous to be modified under such circumstances, but c this has not been a high writing schedule, but posix has got a strdup (3) . Then error code , and then changed may to shall . Then cppreference's statement is out of order. Maybe you should change the cppreference.. Update: it has been changed.

so sing with me:

< H2 > Pointer is shit, C is shit . < / H2 > < H2 > Update: < / H2 >

charge the wording: in the standard (n4741)

clipboard.png

diff can see
http://www.open-std.org/jtc1/.

here.

the compilation and execution of this code itself is error-free (in my default initial compiler environment), but in your environment, there are errors, which may be due to this reason. C2440 can occur because of conformance changes to the compiler in Visual Studio 2015 Update 3.

according to this error description document, I opened the / Zc:strictStrings compiler option, so the error you mentioned will occur. So, you should have turned on that option by default, according to the link above.

the problem with your code is type conversion. Using char sname [] = "no name" , if the program is running and modifies the contents of the sname character array, there will be a problem. If there is no modification, there will be no problem. But in writing code, this is something to pay attention to.

Menu