-1

I am using SFML to create a space invaders clone with C++. I am quite new to C++ and I'm learning it as I'm working on this project.

I am getting the following error when trying to create a new enemy using the constructor and array to set settings for each created enemy:

1>enemy.cpp
1>C:\Users\dzamm\source\repos\SFML\enemy.cpp(35,1): error C2280: 'Enemy &Enemy::operator =(const Enemy &)': attempting to reference a deleted function
1>C:\Users\dzamm\source\repos\SFML\enemy.h(55): message : compiler has generated 'Enemy::operator =' here
1>C:\Users\dzamm\source\repos\SFML\enemy.h(55,1): message : 'Enemy &Enemy::operator =(const Enemy &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'sf::RenderWindow &sf::RenderWindow::operator =(const sf::RenderWindow &)'

Enemy Constructor:

Enemy alienArray[NUMBER_OF_ALIENS];

//constructor sets ID number, loads sprite
Enemy::Enemy(const int id, float sp)
{
    //set alive
    mIsAlive = true;

    //set speed
    enemySpeed = sp;

    // Load an enemy texture
    if (!mEnemyTexture.loadFromFile("Media/Textures/PixelSpaceships/red_01.png")) {
        // Handle loading error
        std::cout << "Oh No! File not loaded." << std::endl;
    }

    //scale sprite and set texture so we know size
    enemySprite.setTexture(mEnemyTexture);
}

Enemy loader method

void Enemy::loader(int noOfAliens) { // might add xPosition;yPosition

    for (int i = 0; i < noOfAliens; i++)
    {
        Enemy alien(i, 10.f); // speed issue
        alien.setLocation(i * 100.f + 50.f, alien.getSprite().getGlobalBounds().height / 2);
line 35     alienArray[i] = alien;
        //alienArray.push_back(alien);
    }
}

1>C:\Users\dzamm\source\repos\SFML\enemy.cpp(35,1): error C2280: 'Enemy &Enemy::operator =(const Enemy &)': attempting to reference a deleted function

Enemy Header file:

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#pragma once

#define NUMBER_OF_ALIENS 4

//class Game;
class Enemy
{
public:
    //constructor sets ID number, loads sprite
    Enemy() {};
    Enemy(const int, float);
    sf::Sprite&             getSprite();
    void                    kill();
    bool                    isAlive();
    void                    load();
    void                    loader(const int noOfAliens);
    void                    setLocation(float xPosition, float yPosition);
    void                    enemyInstantiator(int noOfEnemies, float xPosition, float yPosition);
    void                    update();
    void                    render(sf::RenderWindow& window);

private:

    void                    enemyBehaviour(std::vector<sf::Sprite>& enemyList);
    void                    enemyMovement(std::vector<Enemy>& enemyList);

private:


    sf::Texture             mEnemyTexture;
    sf::Sprite              enemySprite;


    //std::vector<Enemy>        alienArray;
    //std::vector<sf::Sprite> mEnemies;
    sf::RectangleShape      mEnemyBounds;

    sf::RenderWindow        mWindow;


    bool                    mIsShooting;
    bool                    mIsAlive;
    float                   enemySpeed;
};

I read on forums that issue is related to working with a copy of the object instead of the original object, however I am not sure how to handle this to reference the original object I'm modifying.

To reproduce create a constructor for Enemy.cpp class, create an entity Enemy alien using for loop in method as shown in Enemy::loader and store the created entity in the Enemy alienArray as shown by index.

firepro20
  • 63
  • 1
  • 8
  • 1
    Did you also read on this forum that all questions of the kind "my program is not working" must meet all requirements of a [mre]? Your question, unfortunately, fails to meet the requirements for a [mre], and because of that nobody will be able to figure out why this code doesn't work. – Sam Varshavchik Nov 21 '19 at 02:58
  • 1
    You seem to have neglected to copy the error message into your question. You lead up to where it should be by saying "the following error", but the thing that follows is not an error; it's "Enemy Constructor". A direct copy of the error message with no interpretation from you is usually best. (Also, working on a [mcve] is more likely to help you learn C++ than an answer to your question in its current form.) – JaMiT Nov 21 '19 at 03:04
  • `RenderWindow` is non-copyable, you can create one instance of this class in your program, then store just reference to this one. `Enemy` instances should keep pointer/reference to that. – rafix07 Nov 21 '19 at 06:40
  • I have updated based on your suggestions and provided more information, including error message. I have also written how to reproduce problem. Thanks for your help! – firepro20 Nov 21 '19 at 11:49

1 Answers1

0

The issue was related to an sf::RenderWindow reference that was not being even utilised in the code.

sf::RenderWindow &sf::RenderWindow::operator =(const sf::RenderWindow &)

Once I removed this all errors disappeared.

firepro20
  • 63
  • 1
  • 8