Home > Back-end >  C + + write a constructor is wrong for help
C + + write a constructor is wrong for help

Time:10-07

#include
#include
using namespace std;

The class person
{
Public:
The person (s)//oneself write a constructor, feel no problem,
{//but create the object below error
Name="10";
Telenum="20";
}
String name;
String telenum;
};

Int main ()
{
The person p1;
P1. Name="123";
The person (p2);
P2. Telenum="456";//here is ok

The person p3={" 1 ", "2"};//the problem comes, an error here,
//see picture
}

Saying is the const char do not match, but this is not a string?

If the constructor deleted I write my own is not an error,

Check for a long time to check out the reason, strives for the big help, thank you

CodePudding user response:

The person p3={" 1 ", "2"};
Error has been prompt you ah, no corresponding constructor,

CodePudding user response:

And I have never seen such usage, never know the initialization list to the class can use,

CodePudding user response:

You didn't write to accept two parameter constructor ah ~ ~ ~ need to add such a constructor to just go ~
Person (string n, string t) : name (n), telenum (t) {}

CodePudding user response:

reference coo135 reply: 3/f
you didn't write to accept two parameter constructor ah ~ ~ ~ need to add such a constructor to just go ~
The person (string n, string t) : name (n), telenum (t) {}

I, I don't understand, ah, this place and the struct grammar is different, I remember struct when creating an object is similar to
Struct s1={1 value, member 2 value,,,,} here on creating objects of the class parameter of the braces?

CodePudding user response:

M0_44977681
reference 4 floor response:
Quote: refer to the third floor coo135 response:
you didn't write to accept two parameter constructor ah ~ ~ ~ need to add such a constructor to just go ~
The person (string n, string t) : name (n), telenum (t) {}

I, I don't understand, ah, this place and the struct grammar is different, I remember struct when creating an object is similar to
Struct s1={1 value, member 2 value,,,,} here on creating objects of the class parameter of the braces?


Because you wrote the constructor person (), so I need to call the constructor to initialize the members,

C + +, struct members of the public by default, members of a class is private by default, the other no difference,

CodePudding user response:

reference 5 floor coo135 reply:
Quote: refer to 4th floor m0_44977681 response:

Quote: refer to the third floor coo135 response:
you didn't write to accept two parameter constructor ah ~ ~ ~ need to add such a constructor to just go ~
The person (string n, string t) : name (n), telenum (t) {}

I, I don't understand, ah, this place and the struct grammar is different, I remember struct when creating an object is similar to
Struct s1={1 value, member 2 value,,,,} here on creating objects of the class parameter of the braces?


Because you wrote the constructor person (), so I need to call the constructor to initialize the members,

C + +, struct members of the public by default, members of a class is private by default, the other no difference,

Understand, thank you,
  • Related