Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
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());
	    }
    }


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"

C++
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:

C++
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;
    };


Thanks in advance.

What I have tried:

change the declaration of the constructor, in Weapon and in Engine
Posted
Updated 13-Apr-18 22:21pm
v2

1 solution

you need also to implement this constructors because of the
C++
Weapon weapon;//object with default constructor
Weapon egg;
You need such code in the implementation
C++
Weapon::Weapon()
{
  //set some useful defaults
}
Advanced technique: make pointers of the member and construct it somewhere else.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900