c++ error C2512:没有合适的默认构造函数可用
2012-10-10 11:27
295 查看
这是我最近学习c++过程中遇到的一个问题,同时也说明了自定义类的使用,这里借用别人的例子来说明一下这个问题。
View Code
注释掉Weapon的void构造函数会提示error C2512: “Weapon”: 没有合适的默认构造函数可用。
1、由于你在Weapon中定义了其他构造函数,那么,编译器不会为你创建默认构造函数;然而,你在Actor的构造函数中没有调用Weapon的构造函数,那么,编译器会调用Weapon的默认构造函数,然而,却没有定义,所以,产生了“error C2512: “Weapon”: 没有合适的默认构造函数可用”错误!
View Code
#include "stdafx.h" using namespace std; /* --- --- --- --- --- --- --- 武器类 --- --- --- --- --- --- */ class Weapon { private: string name; int power; public: void Show(); //Weapon(){}; Weapon(string name,int power); }; Weapon::Weapon(string name, int power) { this->name = name; this->power = power; }; void Weapon::Show() { cout<<"武器名:"<<this->name<<"威力值:"<<this->power<<endl; }; /* --- --- --- --- --- --- --- 角色类 --- --- --- --- --- --- */ class Actor { private: string name; bool gender; Weapon weapon; public: Actor(string name, bool gender); void Say(); void Say(string message); void SetWeapon(Weapon &weapon) { this->weapon = weapon; } }; Actor::Actor(string name, bool gender) { this->gender = gender; this->name = name; } void Actor::Say() { cout<<"我乃"<<this->name<<"是也..."<<endl; }; void Actor::Say(string message) { cout<<this->name<<":"<<message<<endl; };
注释掉Weapon的void构造函数会提示error C2512: “Weapon”: 没有合适的默认构造函数可用。
1、由于你在Weapon中定义了其他构造函数,那么,编译器不会为你创建默认构造函数;然而,你在Actor的构造函数中没有调用Weapon的构造函数,那么,编译器会调用Weapon的默认构造函数,然而,却没有定义,所以,产生了“error C2512: “Weapon”: 没有合适的默认构造函数可用”错误!
相关文章推荐
- error C2512: “Rectangle”: 没有合适的默认构造函数可用
- Qt错误提示:没有合适的默认构造函数可用
- C++中定义结构体对象提示没有合适的默认构造函数可用错误
- 没有合适的默认构造函数可用 QT
- ui:没有合适的默认构造函数可用
- 没有合适的默认构造函数可用
- error C2512: “HelloWorld”: 没有合适的默认构造函数可用
- error C2512: “HelloWorld”: 没有合适的默认构造函数可用
- 这里出现了没有合适的默认构造函数可用
- 没有合适的构造函数可用??
- Qt程序,可能会提示没有合适的默认构造函数。
- 没有合适的默认构造函数-超前引用
- VMware中安装系统提示没有可用的映像(No image available)
- SSIS 错误代码 没有可用的 OLE DB 访问接口 SSIS 的 64 位版本中不支持 Excel 连接管理器
- ubuntu14.04 Samba服务无法访问 可能没有权限 指定的网络名不再可用的问题
- WPF中异步更新Command可用逻辑时按钮状态没有更新
- 没有银弹,合适与正确的距离
- Android设备中, 可用内存没有宣传的那么多, 为什么?
- springboot的findOne方法没有合适的参数使用
- ubuntu 14.04 64位版本 gcc无法运行 安装ia32-libs提示没有可用的软件包 怎么破