类可以定义在函数体内吗?

cangshu 2010-09-10 01:28:31
比如下面这段代码是可以编译运行的:

#include <iostream>
using namespace std;

int main()
{
class c
{
public:
int a;
};

c cc;

cc.a=0;
cout<<cc.a;
return 0;
}

但如果我定义一个该类的list,编译时就会要求提供Allocator
例如下面的代码:

#include <iostream>
#include <list>
using namespace std;

int main()
{
class c
{
public:
int a;
};

c cc;
cc.a=0;

list<c>c_list;
c_list.push_back(cc);

cout<<c_list.begin()->a;
return 0;
}
//编译会有提示:
//t2.cpp: In function ‘int main()’:
//t2.cpp:16: error: template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main()::c’
//t2.cpp:16: error: trying to instantiate ‘template<class _Alloc> class std::allocator’
//t2.cpp:16: error: template argument 2 is invalid

另外,还有一个问题:
如果将类定义在函数体内,那么类的成员函数是否只能定义在类之内呢?
就像下面这样:

#include <iostream>
using namespace std;

int main()
{
class c
{
public:
void f(){cout<<"hello wrold!";};
};

c cc;
cc.f();

return 0;
}


...全文
713 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
noahnee 2010-09-10
  • 打赏
  • 举报
回复
看了半天,原来是C++
冻结 2010-09-10
  • 打赏
  • 举报
回复
嵌套类。
cangshu 2010-09-10
  • 打赏
  • 举报
回复
感谢pengzhixi(1楼,2楼)和ri_aje(3楼)的解答

晚些回来节贴
fo1_sky 2010-09-10
  • 打赏
  • 举报
回复
没问题
csucdl 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 cangshu 的回复:]
比如下面这段代码是可以编译运行的:
C/C++ code

#include <iostream>
using namespace std;

int main()
{
class c
{
public:
int a;
};

c cc;

cc.a=0;
cout<<cc.a;
return 0……
[/Quote]

楼主, 我用vs 2008build成功
csucdl 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 csucdl 的回复:]
引用楼主 cangshu 的回复:
比如下面这段代码是可以编译运行的:
C/C++ code

#include <iostream>
using namespace std;

int main()
{
class c
{
public:
int a;
};

c cc;

cc.a=0;
cout<<cc.a;
return 0……


楼主, 我用……
[/Quote]

楼主是不是给错分了? 哈 哈
ri_aje 2010-09-10
  • 打赏
  • 举报
回复
问题一,参见 C++03 14.3.1.2

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter. [Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ }; // error: local type used as template-argument
X<S> x3; // error: pointer to local type used as template-argument
X<S*> x4;
}
—end example] [Note: a template type argument may be an incomplete type (3.9). ]

简而言之:局部类不能做模板参数。
注意此限制在C++0x中已经取消了。

问题二,参见 C++03 9.8.2

An enclosing function has no special access to members of the local class; it obeys the usual access rules (clause 11). Member functions of a local class shall be defined within their class definition, if they are defined at all.

简而言之:局部类成员函数必须定义在该类内。
此条在C++0x中仍然存在。
ri_aje 2010-09-10
  • 打赏
  • 举报
回复
不足挂齿,请勿介意,我有此问并非争分,主要还是想确认楼主的本意,怕我引经据典跑C++标准的回答方式招人烦。
pengzhixi 2010-09-10
  • 打赏
  • 举报
回复
是的,如果要定义的话,那么成员函数也必须在类里面给出完整定义。
pengzhixi 2010-09-10
  • 打赏
  • 举报
回复
c++标准规定:A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall
not be used as a template-argument for a template type-parameter
cangshu 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ri_aje 的回复:]

敢问楼主怎么结的帖?是我这里显示错了吗,怎么感谢了还给个0份!
[/Quote]
不好意思,手误...
http://topic.csdn.net/u/20100910/13/58a2e616-abbe-4326-b492-80e6f746d35e.html中奉上40分,请前往回帖
ri_aje 2010-09-10
  • 打赏
  • 举报
回复
敢问楼主怎么结的帖?是我这里显示错了吗,怎么感谢了还给个0份!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧