代码有什么问题? [英] What's wrong withthe code?

查看:68
本文介绍了代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码没问题:


class CSingle

{

public:

static CSingle membs;

int MEM_NUM;

CSingle(int n)

{

}

};


CSingle CSingle :: membs(5);


但是如果它被改为:


class CSingle

{

public:

static CSingle membs;

int MEM_NUM;

CSingle()

{

}

};


CSingle CSingle :: membs();


编译器吐出错误:


错误C2063:''membs'':不一个函数

错误C2040:''membs'':''class CSingle(void)''与''类的

间接水平不同


有谁知道问题的原因?

这是一个实现Singleton的好方法吗?谢谢!

This code is okay:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle(int n)
{
}
};

CSingle CSingle::membs(5);

However if it''s changed as:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle()
{
}
};

CSingle CSingle::membs();

Compiler spits errors:

error C2063: ''membs'' : not a function
error C2040: ''membs'' : ''class CSingle (void)'' differs in levels of
indirection from ''class

Does anyone know the reason for the problem?
Is this a good way to implement Singleton? Thanks!

推荐答案

" Dart" <哒****** @ dickto.com>在消息中写道

新闻:IT ********************* @ bgtnsc04-news.ops.worldnet.att.net
"Dart" <da******@dickto.com> wrote in message
news:IT*********************@bgtnsc04-news.ops.worldnet.att.net
这段代码没问题:

类CSingle
{
公开:
静态CSingle membs;
int MEM_NUM;
CSingle(int n)
{
}
};

CSingle CSingle :: membs(5);

然而如果它'改为:

上课CSingle
{
公开:
静态CSingle membs;
int MEM_NUM;
CSingle()

编译器吐出错误:
This code is okay:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle(int n)
{
}
};

CSingle CSingle::membs(5);

However if it''s changed as:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle()
{
}
};

CSingle CSingle::membs();

Compiler spits errors:




放下括号,即使它成为


CSingle CSingle :: membs;

-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发帖)



Drop the brackets, i.e., make it

CSingle CSingle::membs;
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


On Sun,2004年4月18日04:56:08 GMT in comp.lang.c ++,Dart

< da ****** @ dickto.com>写道,
On Sun, 18 Apr 2004 04:56:08 GMT in comp.lang.c++, "Dart"
<da******@dickto.com> wrote,
CSingle CSingle :: membs();

编译器吐出错误:

错误C2063:''membs'':不是函数
CSingle CSingle::membs();

Compiler spits errors:

error C2063: ''membs'' : not a function




确实。

CSingle CSingle :: membs; //先前声明的静态成员变量

CSingle CSingle :: membs(); //没有args的未声明的成员函数。



Indeed.
CSingle CSingle::membs; // static member variable previously declared
CSingle CSingle::membs(); // undeclared member function with no args.


John Carson< do *********** @ datafast.net.au> ;在消息中写道

news:40 ****** @ usenet.per.paradox.net.au ...
John Carson <do***********@datafast.net.au> wrote in message
news:40******@usenet.per.paradox.net.au...
" Dart" <哒****** @ dickto.com>在消息中写道
新闻:IT ********************* @ bgtnsc04-news.ops.worldnet.att.net
"Dart" <da******@dickto.com> wrote in message
news:IT*********************@bgtnsc04-news.ops.worldnet.att.net
这段代码没问题:

类CSingle
{
公开:
静态CSingle membs;
int MEM_NUM;
CSingle(int n )
{
}
};

CSingle CSingle :: membs(5);

但如果它被改为:

上课CSingle
{
公开:
静态CSingle membs;
int MEM_NUM;
CSingle()
{< SCingle CSingle :: membs();

编译器吐出错误:
This code is okay:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle(int n)
{
}
};

CSingle CSingle::membs(5);

However if it''s changed as:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle()
{
}
};

CSingle CSingle::membs();

Compiler spits errors:



放下括号,即使它成为CSingle CSingle :: membs;



Drop the brackets, i.e., make it

CSingle CSingle::membs;



这是我为了摆脱编译错误而做的,虽然我不明白。


为什么CSingle :: membs(5)使用构造函数CSingle(int n),但是

" CSingle :: membs()"没有CSingle()?


静态CSingle membs是什么意思?真的在这里做魔术吗?谢谢!


It was what I did to rid the compile error though I did not understand.

Why did "CSingle::membs(5)" work with constructor "CSingle(int n)", but
"CSingle::membs()" did not with "CSingle()"?

How does "static CSingle membs;" really do the magic here? Thanks!


这篇关于代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆