64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
int main()
{
class c
{
public:
int a;
};
c cc;
cc.a=0;
cout<<cc.a;
return 0;
}
#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;
}
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). ]
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.