[英]No default constructor exists for class but non default passed
My Thing class is derived from Entity class which has the following constructor 我的Thing类是从Entity类派生的,该类具有以下构造函数
Entity::Entity(string sNames, double xcord, double ycord)
:m_sName(sNames), m_dX(xcord),m_dY(ycord){}
Thing's constructor is 东西的构造函数是
Thing::Thing(string sName, double xcord, double ycord):
Entity(sName, xcord, ycord),
m_iHealth(100),m_Weapon(Weapon("Fists", false, 10.0, 5, 1.0, xcord, ycord)){}
The problem is I get error "no appropriate default constructor available" on my Thing constructor. 问题是我的Thing构造函数出现错误“没有合适的默认构造函数可用”。 What is the problem I specified to use my Entity constructor not a default. 我指定使用我的Entity构造函数而不是默认值的问题是什么? To make the problem even more confusing for me I have another class derieved from Entity that works 为了使问题更令我困惑,我从Entity处提取了另一个有效的类
Weapon::Weapon(string sName, bool iMagical, double dRange, int iDamage,double
dRadius, double dSpawnX, double dSpawnY):
Entity(sName, dSpawnX, dSpawnY), m_bMagical(iMagical), m_dRange(dRange), m_iDamage(iDamage),
m_dRadius(dRadius)
{
}
That runs without error but it appears to be the same exact thing as my Thing constructor with more variables. 那运行没有错误,但是看起来和我的Thing构造函数具有更多的变量完全一样。 I'm sure I am missing something small but I have been stumped for a while. 我确定我缺少一些小东西,但是已经被困了一段时间。
You were correct there was some leftover code that didn't get commented out. 您是正确的,有一些未注释掉的剩余代码。 It seems strange that and error in member variable deceleration shows up in constructor, but thanks anyway. 构造函数中出现成员变量减速度和错误似乎很奇怪,但是无论如何还是要感谢。 It's always the simple things hat get me. 这总是让我容易的事情。
Perhaps your Thing
does not, in fact, have a default constructor and one is required. 实际上,您的Thing
可能没有默认的构造函数,并且是必需的。 A program like this might generate an error like you saw. 这样的程序可能会生成您所看到的错误。
#include <string>
using std::string;
struct Entity {
std::string m_sName;
double m_dX, m_dY;
Entity(std::string, double, double);
};
struct Weapon : Entity {
bool m_bMagical;
double m_dRange;
int m_iDamage;
double m_dRadius;
Weapon(std::string, bool, double, int, double, double, double);
};
struct Thing : Entity {
int m_iHealth;
Weapon m_Weapon;
Thing(std::string, double, double);
};
// OP's code starts here
Entity::Entity(string sNames, double xcord, double ycord)
:m_sName(sNames), m_dX(xcord),m_dY(ycord){}
Thing::Thing(string sName, double xcord, double ycord):
Entity(sName, xcord, ycord),
m_iHealth(100),m_Weapon(Weapon("Fists", false, 10.0, 5, 1.0, xcord, ycord)){}
Weapon::Weapon(string sName, bool iMagical, double dRange, int iDamage,double
dRadius, double dSpawnX, double dSpawnY):
Entity(sName, dSpawnX, dSpawnY), m_bMagical(iMagical), m_dRange(dRange), m_iDamage(iDamage),
m_dRadius(dRadius)
{
}
// OP's code ends here
int main () {
Thing th;
}
Under g++, the precise error is: 在g ++下,精确的错误是:
th.cc: In function ‘int main()’:
th.cc:40: error: no matching function for call to ‘Thing::Thing()’
th.cc:27: note: candidates are: Thing::Thing(std::string, double, double)
th.cc:16: note: Thing::Thing(const Thing&)
Assuming Thing::m_Weapon
is declared as Weapon
and not as Weapon*
, Weapon&
, or equivilent, then you need to change this: 假设Thing::m_Weapon
被声明为Weapon
而不是Weapon*
, Weapon&
或等价物,那么您需要更改以下内容:
m_Weapon(Weapon("Fists", false, 10.0, 5, 1.0, xcord, ycord))
To this 对此
m_Weapon("Fists", false, 10.0, 5, 1.0, xcord, ycord)
You are constructing a temp Weapon
and then trying to construct m_Weapon
from that temp. 您正在构造一个临时Weapon
,然后尝试从该临时构造m_Weapon
。 Presumably Weapon
does not have a copy constructor that takes a Weapon
as input. 据推测, Weapon
没有将Weapon
作为输入的复制构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.