在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a

简介: 在main函数中创建新对象时出错 No enclosing instance of type ooo is accessible. Must qualify the allocation with a


错误原因:No enclosing instance of type ooo is accessible. Must qualify the allocation with an enclosing instance of type ooo (e.g. x.new A() where x is an instance of ooo).


方法:

方法一:分为两个class 不用内部类


方法二:给内部类加个static,因为main方法是静态的。


在一个类的静态成员中去访问非静态成员之所以会出错是因为在类的非静态成员不存在的时候静态成员就已经存在了,访问一个内存中不存在的东西当然会出错。



可以参看博客:https://blog.csdn.net/qq_41885010/article/details/80516284

https://www.cnblogs.com/runnigwolf/p/5570810.html



相关文章
VS2005 error C2864 only static const integral data members can be initialized within a class
VS2005 error C2864 only static const integral data members can be initialized within a class
|
10月前
|
数据库 OceanBase
RuntimeError: 'PluginContext' object has no attribute 'set_variable'
RuntimeError: 'PluginContext' object has no attribute 'set_variable'
84 2
undefined reference to `swr_init+
undefined reference to `swr_init+
90 0
|
Java C++
程序运行问题排查和解决:an instance of ‘std::logic_error‘what(): basic_string::_M_construct null not valid
程序运行问题排查和解决:an instance of ‘std::logic_error‘what(): basic_string::_M_construct null not valid
997 0
|
程序员 编译器 C语言
解决办法:undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3
解决办法:undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3
766 0
Q_OBJECT 导致 error: undefined reference to `vtable for XXXX' 错误
Q_OBJECT 导致 error: undefined reference to `vtable for XXXX' 错误
526 0
No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px '.SF NS Text'} No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing instance of type SmsUtils (e.g. x.new A() where x is an instance of SmsUtils). 今天在写一个短信发送的工具类时使用到了内部类,在实例化内部类时遇到此错误。
1421 0