马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位鱼油们好!
最近在学C++, 在学到构造函数时,按书上的例子跟着做,编译报错。
源码如下:#include <iostream>
#include <string>
using namespace std;
class Stud
{
private:
int num;
char name[20];
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');
|