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) {}