鱼C论坛

 找回密码
 立即注册
查看: 3414|回复: 2

[已解决]C++ 构造函数编译出错

[复制链接]
发表于 2017-10-11 16:34:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位鱼油们好!
最近在学C++, 在学到构造函数时,按书上的例子跟着做,编译报错。
源码如下:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. class Stud
  5. {
  6.     private:
  7.         int num;
  8.         char name[20];
  9.         char sex;

  10.     public:
  11.         Stud(int n, char nam[], char s)
  12.         {
  13.                 num = n;
  14.                 strcpy(name, nam);
  15.                 sex = s;
  16.         }
  17.         ~Stud()
  18.         {}
  19.         //stud stud(int num,char name[],char sex);
  20.         void display()
  21.         {
  22.                 cout << "Num: " << num << endl;
  23.                 cout << "Name: " << name << endl;
  24.                 cout << "Sex: " << sex << endl;
  25.         }

  26. };

  27. int main()
  28. {
  29.         Stud stud1 = (10010, "XiaoMing",'M');
  30.         Stud stud2 = (10011, "HanMeimei", 'F');
  31.         stud1.display();
  32.         stud2.display();

  33.         return 0;
  34. }
复制代码


报错如下:
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


还请各位帮忙看看是什么原因,谢谢~
最佳答案
2017-10-11 16:38:33
Stud stud1(10010, "XiaoMing",'M');
Stud stud2(10011, "HanMeimei", 'F');
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-11 16:38:33 | 显示全部楼层    本楼为最佳答案   
Stud stud1(10010, "XiaoMing",'M');
Stud stud2(10011, "HanMeimei", 'F');
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-11 16:59:55 | 显示全部楼层
橙C 发表于 2017-10-11 16:38
Stud stud1(10010, "XiaoMing",'M');
Stud stud2(10011, "HanMeimei", 'F');

  居然错在粗心上  谢谢谢谢~~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-30 14:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表