类中const数组初始化,遇到编译错误must be initialized in constructor base/member initializer list
类中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




