“武器”:没有适当的默认构造函数可用 [英] "Weapon": no appropriate default constructor availa
问题描述
I don't get it. I've been staring at the code the code for a few hours and I don't know how I can fix it.
The class I'm creating, called Weapon.
This is how I defined the constructor:
<pre lang="c++">template <class T1> Weapon (T1& A);
这是我实现构造函数的方式:
This is how I implement the constructor:
template<class T1> Weapon::Weapon(T1 & A)
{
if(A == Chicken)
{
(*texture).loadFromFile("Images/Egg.png");
sprite.setTexture(*texture);
sprite.setPosition((*(chickens.begin())).getPosition().x, (*(chickens.begin())).getPosition().y);
}
else if (A == Ship)
{
(*texture).loadFromFile("Images/Weapon.png");
sprite.setTexture(*texture);
sprit<code></code>e.setPosition(ship.getPosition());
}
}
我有错误:
C2512武器:没有适当的默认构造函数
当我点击它时,另一个一个出现:
E0291类武器没有默认构造函数
并将我重定向到文件Engine.cpp
And I have error:
C2512 "Weapon": no appriopriate default constructor available
When I click on it, another one shows up:
E0291 no default constructor exists for class "Weapon"
and redirects me to the file "Engine.cpp"
Engine::Engine(RenderWindow & window)
{
if (!background.loadFromFile("data/images/background.png"))
{
MessageBox(NULL, "Textures not found!", "ERROR", NULL);
return;
}
runEngine(window);
}
并指向开头大括号。
我的班级引擎看起来像这样:
and points to the opening brace.
My class Engine looks like this:
class Engine
{
public:
Engine(RenderWindow &win);
~Engine(void);
void runEngine(RenderWindow &window);
template <class T1, class T2> bool isIntersecting(T1& A, T2& B);
template <class T1, class T2> bool collisionTest(T1& A, T2& B);
private:
Ship ship;
Weapon weapon;
Weapon egg;
vector<Chicken> chickens;
Texture background;
};
提前致谢。
< b>我尝试过:
Thanks in advance.
What I have tried:
change the declaration of the constructor, in Weapon and in Engine
推荐答案
你还需要实现这个构造函数,因为
you need also to implement this constructors because of the
Weapon weapon;//object with default constructor
Weapon egg;
您需要这样的代码在实施中
You need such code in the implementation
Weapon::Weapon()
{
//set some useful defaults
}
高级技巧:制作成员的指针并在其他地方构建它。
Advanced technique: make pointers of the member and construct it somewhere else.
这篇关于“武器”:没有适当的默认构造函数可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!