VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1133|回复: 5

不聚合带用户定义的构造函数的类型

[复制链接]
58_avatar_middle
最佳答案
0 
在线会员 发表于 2021-2-24 11:48:49 | 显示全部楼层 |阅读模式
using namespace std;
class CStudent
{
public:
        char name[50];
        char sex;
        int  age;
        int  num;
public:
        CStudent(char* pname,char t_sex,int t_age,int t_num);
        ~CStudent(void);
};

#include "stdafx.h"
#include "Student.h"



CStudent::CStudent(char* pname,char t_sex,int t_age,int t_num)
{
        strcpy_s(name,pname);
        sex=t_sex;
        age=t_age;
        num=t_num;
}

CStudent::~CStudent(void)
{
}


#include "stdafx.h"
#include "Student.h"


int _tmain(int argc, _TCHAR* argv[])
{
        CStudent stu1={"zhangsan",'m',23,1000};       
       
        return 0;
}


error C2552: “stu1”: 不能用初始值设定项列表初始化非聚合
1>          “CStudent”: 不聚合带用户定义的构造函数的类型!!!!!!!!!!!!!!!!!!!!!!!!!!!

求助

求助




上一篇:如何学习一个框架
下一篇:ch[1] = 10;
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-2-26 15:20:06 | 显示全部楼层
搞清楚数据类型、数据类型转换,基础不牢,学到后边问题越多,不要小看这些基础的东西!

  1. #include <iostream>
  2. using namespace std;

  3. class CStudent
  4. {
  5. public:
  6.         char name[50];
  7.         char sex;
  8.         int  age;
  9.         int  num;
  10. public:
  11.         CStudent(char* pname, char t_sex, int t_age, int t_num);
  12.         ~CStudent(void);
  13. };

  14. CStudent::CStudent(char* pname, char t_sex, int t_age, int t_num)
  15. {
  16.         strcpy_s(name, pname);
  17.         sex = t_sex;
  18.         age = t_age;
  19.         num = t_num;
  20. }

  21. CStudent::~CStudent(void)
  22. {
  23. }

  24. int main()
  25. {
  26.         CStudent stu = { (char*)"张三", 'm', 23,1000 };
  27.         cout << "stu:" << endl;
  28.         cout << stu.name << endl;
  29.         cout << stu.sex << endl;
  30.         cout << stu.age << endl;
  31.         cout << stu.num << endl;

  32.         char name[50] = "李四";
  33.         CStudent stu1 = { name, 'm', 23,1000 };
  34.         cout << "stu1:" << endl;
  35.         cout << stu1.name <<endl;
  36.         cout << stu1.sex <<endl;
  37.         cout << stu1.age <<endl;
  38.         cout << stu1.num <<endl;

  39.         cout << "---------\nstu2:" << endl;
  40.         CStudent sut2(name, 'm', 23,1000);
  41.         cout << sut2.name << endl;
  42.         cout << sut2.sex << endl;
  43.         cout << sut2.age << endl;
  44.         cout << sut2.num << endl;

  45.         cout << "---------\nstu3:" << endl;
  46.         CStudent sut3((char*)"王五", 'm', 23, 1000);
  47.         cout << sut3.name << endl;
  48.         cout << sut3.sex << endl;
  49.         cout << sut3.age << endl;
  50.         cout << sut3.num << endl;

  51.         return 0;
  52. }
复制代码

评分

参与人数 1驿站币 +2 热心值 +2 收起 理由
58_avatar_small thzzl + 2 + 2 很给力!

查看全部评分

58_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-2-24 16:46:59 | 显示全部楼层
求大佬给予帮助,急急!!
70_avatar_middle
最佳答案
49 
在线会员 发表于 2021-2-25 09:13:01 | 显示全部楼层
构造函数 第一个参数改成 const 试试 CStudent(const char* pname,char t_sex,int t_age,int t_num)
58_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-2-25 23:28:07 | 显示全部楼层
@tony666    还是不行,报一样的错。这个例子是我看着教学视频手所手一字不差抄下来的,老师在视频上就没问题,我怎么会出这个问题呢??奇了怪了!我用的是VS2012,老师用的是VS2013,难道与版本有关!!!
70_avatar_middle
最佳答案
49 
在线会员 发表于 2021-2-26 09:01:47 | 显示全部楼层
chjxx110 发表于 2021-2-25 23:28
@tony666    还是不行,报一样的错。这个例子是我看着教学视频手所手一字不差抄下来的,老师在视频上就没问 ...

应该是初始化列表跟构造函数参数不匹配,实在不行你就 直接调构造函数呗,用小括号,不用大括号
CStudent stu1("zhangsan",'m',23,1000);
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-8 18:19

Powered by CcTry.CoM

© 2009-2021 cctry.com

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