CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

类中const数组初始化,遇到编译错误must be initialized in constructor base/member initializer list

楼主zb2003(生猛土豆)2005-05-12 10:44:07 在 VC/MFC / 基础类 提问

类中const数组初始化,遇到编译错误must   be   initialized   in   constructor   base/member   initializer   list  
  怎么回事。  
  class   CDlg   :   public   CDialog  
  {  
  const   mystyle   test[3];  
  }  
  CDlg::CDlg(CWnd*   pParent   /*=NULL*/)  
  :   CDialog(CDlg::IDD,   pParent)    
  {  
  test=   {a,b,c};//如果前面不用const,此处也不能通过。  
  }  
  注:mystyle是函数指针类型,a,b,c是函数名。 问题点数:100、回复次数:11Top

1 楼laiyiling(陌生人[MVP])回复于 2005-05-12 10:48:54 得分 10

你可以直接定义为static,在类外面初始化Top

2 楼moany(长枪大戟)回复于 2005-05-12 10:49:12 得分 10

CDlg::CDlg(CWnd*   pParent   /*=NULL*/):test=   {a,b,c};  
  :   CDialog(CDlg::IDD,   pParent)    
  {  
   
  }  
  Top

3 楼theCFan(郁闷的饿猫)回复于 2005-05-12 10:56:53 得分 10

类定义时还没有为类分配任何存储空间(除了static变量),怎么给它初始化呢?  
  在类中,好象只能用static   变量来实现constTop

4 楼dongfa(一桶江湖( http://www.codelive.net ))回复于 2005-05-12 11:08:45 得分 10

应该是不行的吧?如果防止修改的话,可以变为私有成员呀.Top

5 楼viewpl(下了军令状,明年泡不到mm就提着向下小JJ回家叩见祖宗)回复于 2005-05-12 12:48:03 得分 10

class   ClassName  
  {  
  ...  
   
  public:  
          static   const   int   cns[3];  
  ...  
  };  
   
  ...  
  const   ClassName::cns[3]   =   {1,2,3};  
  ...  
   
  Top

6 楼zb2003(生猛土豆)回复于 2005-05-13 11:23:02 得分 0

我用const是想学习一下,各位的方法我都试了,不行啊Top

7 楼bobob(静思)回复于 2005-05-13 11:37:47 得分 10

使用初始化列表Top

8 楼bobob(静思)回复于 2005-05-13 11:44:18 得分 10

class   CDlg   :   public   CDialog  
  {  
  const   char   test;  
  }  
  CDlg::CDlg(CWnd*   pParent   /*=NULL*/)  
  :   CDialog(CDlg::IDD,   pParent),text('a')    
  {  
  }  
  Top

9 楼wshcdr(dd)回复于 2005-05-13 12:41:41 得分 10

楼上的又不是数组  
  我写了一个希望能给楼主启发  
  /////////////////////////  
  #include   <string>  
  #include   <iostream>  
   
  using   namespace   std;  
   
  class     ATry  
  {  
  public   :  
  ATry();  
   
  private:  
    int*   p;  
  public:  
    inline   void   prt()  
    {  
    for   (int   i   =0   ;   i<   3;   i++)  
    cout   <<   p[i]   <<   endl;  
    }  
  static   const   Copy[3];  
  };  
   
  const   int   ATry::Copy[]   =     {1,   2,   3  
  };  
   
  ATry::ATry()  
  {  
  p   =   const_cast<int*>(Copy);  
  }  
   
  void   main  
  ()  
  {  
  ATry   a;  
  a.prt();  
  }  
  ////////////////////////////////////Top

10 楼lzwei3842(赐缘)回复于 2005-05-13 12:43:49 得分 10

UPTop

11 楼vcmute(BCare4 H1Rest Good9!)回复于 2005-05-13 14:00:42 得分 10

Compiler   Error   C2536  
  'identifier1::identifier2'   :   cannot   specify   explicit   initializer   for   arrays  
   
  The   specified   member   of   a   class,   structure,   or   union   could   not   be   initialized.  
   
  This   error   can   be   caused   if   a   constructor   is   not   available   to   initialize   one   or   more   members   of   an   array.   If   the   size   of   the   array   is   greater   than   the   number   of   initializers,   then   a   default   constructor   must   be   defined.  
   
  Alternatively,   this   error   can   be   caused   by   declaring   a   nonstatic   array   with   the   const   specifier.   This   kind   of   array   cannot   be   explicitly   initialized.  
  Top

相关问题

  • 数组初始化
  • 数组初始化
  • 数组初始化
  • 数组的初始化
  • 数组(Array)初始化
  • 关于数组初始化
  • 数组初始化缺省
  • 初始化多维数组
  • 怎样初始化数组??
  • 数组初始化问题.

关键词

  • null
  • cdlg
  • 初始化
  • 数组
  • pparent
  • atry
  • initialized
  • 类
  • cdialog
  • constructor

得分解答快速导航

  • 帖主:zb2003
  • laiyiling
  • moany
  • theCFan
  • dongfa
  • viewpl
  • bobob
  • bobob
  • wshcdr
  • lzwei3842
  • vcmute

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo