C++ 构造函数编译出错
各位鱼油们好!最近在学C++, 在学到构造函数时,按书上的例子跟着做,编译报错。
源码如下:
#include <iostream>
#include <string>
using namespace std;
class Stud
{
private:
int num;
char name;
char sex;
public:
Stud(int n, char nam[], char s)
{
num = n;
strcpy(name, nam);
sex = s;
}
~Stud()
{}
//stud stud(int num,char name[],char sex);
void display()
{
cout << "Num: " << num << endl;
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
}
};
int main()
{
Stud stud1 = (10010, "XiaoMing",'M');
Stud stud2 = (10011, "HanMeimei", 'F');
stud1.display();
stud2.display();
return 0;
}
报错如下:
test35_01.cpp:32:16: warning: expression result unused [-Wunused-value]
Stud stud1 = (10010, "XiaoMing",'M');
^~~~~
test35_01.cpp:32:23: warning: expression result unused [-Wunused-value]
Stud stud1 = (10010, "XiaoMing",'M');
^~~~~~~~~~
test35_01.cpp:32:7: error: no viable conversion from 'char' to 'Stud'
Stud stud1 = (10010, "XiaoMing",'M');
^ ~~~~~~~~~~~~~~~~~~~~~~~
test35_01.cpp:4:7: note: candidate constructor (the implicit copy constructor)
not viable: no known conversion from 'char' to 'const Stud &' for 1st
argument
class Stud
^
test35_01.cpp:33:16: warning: expression result unused [-Wunused-value]
Stud stud2 = (10011, "HanMeimei", 'F');
^~~~~
test35_01.cpp:33:23: warning: expression result unused [-Wunused-value]
Stud stud2 = (10011, "HanMeimei", 'F');
^~~~~~~~~~~
test35_01.cpp:33:7: error: no viable conversion from 'char' to 'Stud'
Stud stud2 = (10011, "HanMeimei", 'F');
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
test35_01.cpp:4:7: note: candidate constructor (the implicit copy constructor)
not viable: no known conversion from 'char' to 'const Stud &' for 1st
argument
class Stud
还请各位帮忙看看是什么原因,谢谢~ Stud stud1(10010, "XiaoMing",'M');
Stud stud2(10011, "HanMeimei", 'F'); 橙C 发表于 2017-10-11 16:38
Stud stud1(10010, "XiaoMing",'M');
Stud stud2(10011, "HanMeimei", 'F');
{:10_266:}居然错在粗心上谢谢谢谢~~~
页:
[1]