0

Please help error: no instance of constructor matches the argument list. And also please help explain about "strcpy(this->name, name);"

class Student {
    char name[50];
    char surname[50];
    int age;
public:
    Student(char name[], char surname[], int age) {
        strcpy(this->name, name); // please explain this line what does it means?
        strcpy(this->surname, surname);
        this->age = age;
    }
    void Show() {
        cout << "Name: " << this->name << endl;
        cout << "Surname: " << this->surname << endl;
        cout << "Age: " << this->age;
    }
};

int main() {
    Student A("Ivan", "Sidoroff", 25);
    A.Show();

    system("pause");
    return 0;
}

enter image description here

Rin Prak
  • 47
  • 8
  • related/dupe: https://stackoverflow.com/questions/2760500/why-is-passing-a-string-literal-into-a-char-argument-only-sometimes-a-compiler – NathanOliver Oct 23 '18 at 14:31
  • 1
    1) The code, that you show, [compiles just fine](https://ideone.com/DWl68A). 2) What `strcpy` does is explained in the documentation of [`strcpy`](https://en.cppreference.com/w/cpp/string/byte/strcpy). – Algirdas Preidžius Oct 23 '18 at 14:32
  • 1
    @AlgirdasPreidžius You need to turn up your warnings then *ISO C++11 does not allow conversion from string literal to 'char *'* – NathanOliver Oct 23 '18 at 14:37
  • 1
    @NathanOliver I thought that on C++11 and later this is an error, and not a warning, and since I compiled with C++14, I thought that if there were any such errors, it wouldn't compile. :/ Curse you g++, and your non-standard extensions! – Algirdas Preidžius Oct 23 '18 at 14:42
  • 1
    @AlgirdasPreidžius If it help I always use `-Wall -pedantic` when compiling. I also use Wandbox or coliru as you can actually specify all the options and see all the warnings. – NathanOliver Oct 23 '18 at 14:43
  • 1
    `-Wall -Wextra -Werror -pedantic` ftw – Lightness Races in Orbit Oct 23 '18 at 14:49

1 Answers1

2

Please help error: no instance of constructor matches the argument list.

Instead of this:

Student(char name[], char surname[], int age) {

Try this:

Student(const char *name, const char *surname, int age) {

It complains because the char pointers aren't matching the pointers to const char.

strcpy(this->name, name); // please explain this line what does it means?

It copies the string from name (the parameter passed) to the other name (that is part of class Student). Since both are called name it's ambiguous. In this case, name refers to the parameter, and this->name is used to refer to the field in the class Student instead.

More generally, this is a pointer to the object calling the function.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Thanks for your answer... I have tried your code, it did fine, but in addition it need to change from strcpy to strcpy_s. it work now. – Rin Prak Oct 23 '18 at 15:38
  • thanks for your explain on strcpy also... in order to avoid ambiguous, I remember my teacher's lesson to use underscore: _name (the parameter passed). – Rin Prak Oct 23 '18 at 15:48