I have two classes as shown below.
When i try to populate a vector of Entity objects in the Spawner Entity class,it gives me this error
error: use of deleted function 'Entity::Entity(Entity&&)'
I cant figure out why it wont call the constructor and push the class object on the vector
Also, I Have inherited the Entity Class in the Spawner Entity
Entity.h
#pragma once
#include<iostream>
#include<vector>
#include<SFML/Window.hpp>
#include<SFML/Graphics.hpp>
class Entity
{
private:
sf::RenderWindow win;
sf::RectangleShape enemy;
public:
Entity();
void InitializeEntity();
void Update(const float* DeltaTime);
void Render(sf::RenderWindow& mWindow);
};
Entity.cpp
Entity::Entity()
{
InitializeEntity();
}
void Entity::InitializeEntity()
{
enemy.setPosition(rand()%255+1,rand()%255+1);
enemy.move(rand()%255+1,rand()%255+1);
enemy.setSize(sf::Vector2f(50.f,50.f));
sf::Color color(rand()%255+1,rand()%255+1,rand()%255+1);
enemy.setFillColor(color);
enemy.setOutlineColor(sf::Color::White);
enemy.setOutlineThickness(2.f);
}
void Entity::Render(sf::RenderWindow& mWindow)
{
mWindow.draw(enemy);
}
SpawnerEntity.h
#pragma once
#include<iostream>
#include<vector>
#include<SFML/Window.hpp>
#include<SFML/Graphics.hpp>
#include "Entity.h"
class SpawnerEntity:public Entity
{
private:
float SpawnTimer;
float SpawnTimerMax;
int MaxEnemies;
bool done;
//std::vector<Entity> SpawnedEnemies;
public:
SpawnerEntity();
void InitializeEntity();
void SpawnSubEntities(std::vector<Entity> &enemies);
bool CheckSpawnDone();
};
SpawnerEntity.cpp
#include "SpawnerEntity.h"
SpawnerEntity::SpawnerEntity()
{
SpawnTimerMax=1.f;
SpawnTimer=SpawnTimerMax;
MaxEnemies=10;
done=false;
}
bool SpawnerEntity::CheckSpawnDone()
{
return done;
}
void SpawnerEntity::SpawnSubEntities(std::vector<Entity> &enemies)
{
for(int i=0;i<MaxEnemies;i++)
{
std::cout << "SPAWNED" << '\n';
enemies.push_back(Entity()); //this line here gives me the above error
}
done=true;
}
Related
This question already has answers here:
Virtual/pure virtual explained
(12 answers)
Closed 1 year ago.
booking.h
#ifndef _BOOKING_H_
#define _BOOKING_H_
#include <string>
class Event {
private:
std::string event_option;
public:
Event(std::string event_option);
virtual ~Event();
void list_specific_event_details();
virtual void list_details();
};
class Films : public Event {
private:
std::string movie_option;
public:
Films(std::string movie_option);
void list_details();
};
class Live_Music : public Event {
private:
std::string live_music_option;
public:
Live_Music(std::string live_music_option);
void list_details();
};
class Standup_Comedy : public Event {
private:
std::string comedy_option;
public:
Standup_Comedy(std::string comedy_option);
void list_details();
};
#endif
booking.cpp
#include "booking.h"
#include <string>
Event::Event(std::string event_option)
{
this->event_option = event_option;
}
Event::~Event()
{
}
void Event::list_specific_event_details()
{
return list_details();
}
Films::Films(std::string movie_option) : Event("Films")
{
this->movie_option = movie_option;
}
void Films::list_details()
{
//some code ...
return;
}
Live_Music::Live_Music(std::string live_music_option) : Event("Live_Music")
{
this->live_music_option = live_music_option;
}
void Live_Music::list_details()
{
//some code...
return;
}
Standup_Comedy::Standup_Comedy(std::string comedy_option) : Event("Standup_Comedy")
{
this->comedy_option = comedy_option;
}
void Standup_Comedy::list_details()
{
//some code...
return;
}
main.cpp
#include "booking.h"
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector <Event *> choice;
choice.push_back(new Films("f"));
choice.push_back(new Live_Music("l"));
choice.push_back(new Standup_Comedy("s"));
for(unsigned i = 0; i < choice.size(); i++)
{
choice[i] -> list_specific_event_details();
}
for (Event * e: choice) delete e;
choice.clear();
}
I have written the above block of codes and got an error when I tried to compile it.
The error is as follows:
usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccuWXzBM.o:booking.cpp:(.rdata$_ZTV5Event[_ZTV5Event]+0x20): undefined reference to `Event::list_details()'
collect2: error: ld returned 1 exit status
Why am I getting this error, and how can I solve it?
You have not defined your method Event::list_details(). If you only want to declare the function but do not want to implement it, that is what is called an Interface, you can make it a pure virtual method with:
class Event
{
...
virtual void list_details() = 0;
...
};
I am using c++11 compiler.
I have two classes - class Test and class TestHelper.
The class Test is a friend-to-class TestHelper.
The class Test is only which we can access from outside.
Now, we want to call Test API i.e. setVal(). This setVal() should call
Test2 API i.e. setX and is expecting this pointer. I don't want to use this pointer but want
to use a smart pointer instead. How can I do so?
The notion of this kind of desirability is because of the fact that in reality, my class Test is pretty big. So, I am trying to make a helper class for Test i.e.
class TestHelper;
class Test
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(this, 324);
}
};
class TestHelper
{
public:
void setX(Test *test, int val) {
/** some algorithm here and then change val to something else */
test->x = val*100;
}
};
int main()
{
std::cout << "Hello World!\n";
Test x;
x.setVal(130);
}
I tried changing the prototype from void setX(Test *test, int val)
to void setX(std::shared_ptr<Test> test, int val) but don't know how to pass this pointer
as std::shared_ptr<Test> test here.
So here is working solution with shared pointers. The example doesn't even compile due to missing definitions so you have to restructure your code into headers and cpp files.
Test.h:
#ifndef TEST_H
#define TEST_H
#include <memory>
#include "TestHelper.h"
class Test : public std::enable_shared_from_this<Test>
{
private:
friend class TestHelper;
int x;
public:
void display();
void setVal(int val);
};
#endif
Test.cpp:
#include <iostream>
#include "Test.h"
void Test::display() {
std::cout << x;
}
void Test::setVal(int val) {
TestHelper testH;
testH.setX(shared_from_this(), 324);
}
TestHelper.h:
#ifndef TESTHELPER_H
#define TESTHELPER_H
class Test;
class TestHelper
{
public:
void setX(std::shared_ptr<Test> test, int val);
};
#endif
TestHelper.cpp:
#include <memory>
#include "TestHelper.h"
#include "Test.h"
void TestHelper::setX(std::shared_ptr<Test> test, int val) {
/** some algorithm here and then change val to something else */
test->x = val*100;
}
main.cpp:
#include <iostream>
#include <memory>
#include "Test.h"
int main(void){
std::cout << "Hello World!\n";
auto x = std::make_shared<Test>();
x->setVal(130);
x->display();
}
You can run it here: https://paiza.io/projects/e/79dehCx0RRAG4so-sVZcQw
I don't understand why you want this, here's a few variants that compile
reference
// Reference variant
#include <iostream>
class Test;
class TestHelper
{
public:
void setX(Test & test, int val);
};
class Test
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(*this, 324);
}
};
void TestHelper::setX(Test &test, int val)
{
/** some algorithm here and then change val to something else */
test.x = val*100;
}
int main()
{
Test x;
x.setVal(130);
x.display();
}
http://cpp.sh/7t3ec
shared ptr
// Shared ptr variant
#include <iostream>
#include <memory> // Required for shared_ptrs
class Test;
class TestHelper
{
public:
void setX(std::shared_ptr<Test> test, int val);
};
class Test : public std::enable_shared_from_this<Test>
{
friend class TestHelper;
int x;
public:
void display() {
std::cout << x;
}
void setVal(int val) {
TestHelper testH;
testH.setX(shared_from_this(), 324);
}
};
void TestHelper::setX(std::shared_ptr<Test> test, int val)
{
/** some algorithm here and then change val to something else */
test->x = val*100;
}
int main()
{
auto x = std::make_shared<Test>(); // x needs to be created as shared_ptr or it won't work
x->setVal(130);
x->display();
}
http://cpp.sh/87ao2
Perhaps with these you can refine your question?
App.h:
#ifndef APP_H
#define APP_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "RoomManager.h"
typedef unsigned int uint;
class App {
private:
sf::RenderWindow window;
sf::Event evt;
uint screen_width = 0;
uint screen_height = 0;
sf::Clock deltaClock;
float deltaTime = 0.0f;
RoomManager& roomManager = RoomManager::GetInstance();
std::vector<sf::Sprite> allDrawables;
App() { }
~App() { }
public:
static App& GetInstance() {
static App instance;
return instance;
}
void CreateWindow(uint width, uint height) {
screen_width = width;
screen_height = height;
window.create(sf::VideoMode(screen_width, screen_height), "Point'n'click adventure");
}
void Update() {
while (window.isOpen()) {
deltaTime = deltaClock.restart().asSeconds();
while (window.pollEvent(evt)) {
if (evt.type == sf::Event::EventType::Closed)
window.close();
}
Draw();
}
}
void ResizeScreen(uint width, uint height) {
screen_width = width;
screen_height = height;
window.setSize(sf::Vector2u(screen_width, screen_height));
}
void AddDrawable(sf::Sprite& sprite) {
allDrawables.push_back(sprite);
}
void Draw() {
window.clear();
for (int i = 0; i < allDrawables.size(); i++) {
window.draw(allDrawables[i]);
}
window.display();
}
};
#endif
DrawableEntity.h:
#ifndef DRAWABLEENTITY_H
#define DRAWABLEENTITY_H
#include <SFML/Graphics.hpp>
#include "App.h"
class DrawableEntity {
private:
sf::Texture backgroundTexture;
sf::Sprite backgroundSprite;
public:
DrawableEntity() {
App::GetInstance().AddDrawable(backgroundSprite);
}
~DrawableEntity() { }
void LoadTexture(const std::string texturePath) {
backgroundTexture.loadFromFile(texturePath);
backgroundSprite.setTexture(backgroundTexture);
}
};
#endif
RoomManager.h:
#ifndef ROOMMANAGER_H
#define ROOMMANAGER_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "Room.h"
class RoomManager {
private:
std::vector<Room> rooms;
public:
static RoomManager& GetInstance() {
static RoomManager instance;
return instance;
}
void AddRoom(Room room) {
rooms.push_back(room);
}
};
#endif
Room.h:
#ifndef ROOM_H
#define ROOM_H
#include <SFML/Graphics.hpp>
#include "App.h"
#include "DrawableEntity.h"
class Room {
private:
DrawableEntity background;
public:
Room(const std::string backgroundTexturePath) {
background.LoadTexture(backgroundTexturePath);
}
~Room() { }
};
#endif
main.cpp:
#include <SFML/Graphics.hpp>
#include "App.h"
using namespace sf;
int main() {
App& app = App::GetInstance();
RoomManager& roomManager = RoomManager::GetInstance();
app.CreateWindow(1920, 1080);
roomManager.AddRoom(Room("room_0_background.jpg"));
app.Update();
return 0;
}
My idea here is that whenever I would create DrawableEntity object, it would automatically add its sprite to allDrawables vector in App. The above code throws me C2653 error saying that "'App' is not a class or namespace name". The error appears in DrawableEntity() constructor, at line where I call AddDrawable() method. It's been a while since I coded in C++ (now I code mostly in C#) and I'm not sure how to fix this.
[Note: As, you haven't shared DrawableEntity() constructor code(I didn't find any) I am assuming your problem might be the following. If the following solution does not solve your issue, please share DrawableEntity() constructor code.]
You must have called AddDrawable() as following:
App& appInstance = App::GetInstance();
appInstance::AddDrawable(); // it is wrong
You shouldn't call like above
Rather do the following:
App& appInstance = App::GetInstance();
appInstance.AddDrawable(); // it is correct
Edit(Possible solution, this must be happening in your code):
I guess #drescherjm is right. Please, take a careful look at the code snippets below. I have tested some codes similiar like your code structure:
so_test.cpp file:
#include <cstdio>
#include "so_header.h"
int main() {
A& objA = A::getInstance();
int xa = objA.getX();
printf("xa: %d\n", xa);
B& objB = B::getInstance();
int xb = objB.getX();
printf("xb: %d\n", xb);
return 0;
}
so_header.h file:
#ifndef CLASS_A
#define CLASS_A
#include <cstdio>
#include "so_header_2.h"
class A {
private:
int x = 100;
A(){}
public:
static A& getInstance() {
static A obj;
return obj;
}
int getX() {
return x;
}
void print() {
B& objB = B::getInstance();
int xb = objB.getX();
printf("xb: %d\n", xb);
}
};
#endif
so_header_2.h:
#ifndef CLASS_B
#define CLASS_B
#include <cstdio>
#include "so_header.h"
class B {
private:
int x = 200;
B(){}
public:
static B& getInstance() {
static B obj;
return obj;
}
int getX() {
return x;
}
void print() {
A& objA = A::getInstance();
int xa = objA.getX();
printf("xa: %d\n", xa);
}
};
#endif
When I compile so_test.cpp, I get the follwowing error(using mingw compiler):
In file included from so_header.h:6:0,
from so_test.cpp:2:
so_header_2.h: In member function 'void B::print()':
so_header_2.h:21:3: error: 'A' was not declared in this scope
A& objA = A::getInstance();
^
so_header_2.h:21:6: error: 'objA' was not declared in this scope
A& objA = A::getInstance();
^
so_header_2.h:21:13: error: 'A' is not a class, namespace, or enumeration
A& objA = A::getInstance();
The reason is in print function of A, A is calling B and in print function of B, B is calling A.
Your code structure follows a similiar pattern. Let's start from DrawableEntity.
> DrawableEntity calls App
> App calls RoomManager
> RoomManager calls Room
> Room calls App and DrawableEntity(notice, the order here. App is called before DrawableEntity, and so loop has come back to App)
> App calls RoomManager...and ...so on...
So, the loop is:
DrawableEntity -> App -> RoomManager -> Room -> App -> RoomManager -> ...
As, App is the start of the loop or the first object in the loop to be called, you're getting error recognizing "App" class...cause, you started declaring App, but you've never finished declaring it, and in the middle of declaring it, you are trying to use it. So, you're basically trying to use something that hasn't even seen the light of existence(An analogy could be: its still in womb, not born yet...).
This has happended with your code.
To solve your problem, you need to reorganize your code structure a bit. Just break the cycle(i.e. loop) of call dependency, and you'd be okay.
This is my enum header file
//use like PostionType::President
enum PositionType
{ President,
VicePresident,
Secretary,
Treasurer,
Normal
};
And in my Ballot paper header and cpp file
#include <list>
#include <iostream>
#include "Candidate.h"
#include "PositionType.h" //include enum
class BallotPaper
{
private:
PositionType _positionbp;
std::list<Candidate> _candidatesbp;
public:
BallotPaper();
void setPositionBP(PositionType positionbp);
PositionType getPositionBP();
void setCandidateBP(std::list<Candidate> candidatesbp);
std::list<Candidate> getCandidateBP();
Candidate getCandidate(int index);
void ShowCandidates();
~BallotPaper();
};
#include "BallotPaper.h"
#include <string>
#include <iostream>
void BallotPaper::setPositionBP(PositionType positionbp)
{
_positionbp = positionbp;
}
PositionType BallotPaper::getPositionBP()
{
return _positionbp;
}
void BallotPaper::setCandidateBP(std::list<Candidate> candidatesbp)
{
_candidatesbp = candidatesbp;
}
std::list<Candidate> BallotPaper::getCandidateBP()
{
return _candidatesbp;
}
void BallotPaper::ShowCandidates()
{
for(Candidate c : _candidatesbp)
{
c->IncreaseVoteCount(); //ERROR!!!!
}
}
and this will be my candidate header and cpp file
class Candidate:public Member
{
private:
int _votecount;
PositionType _position;
public:
Candidate(std::string name, int id, std::string course, int contact, std::string joindate, PositionType currentposition) ;
~Candidate();
void setVoteCount(int votecount);
int getVoteCount();
void setPosition(PositionType position);
PositionType getPosition();
void IncreaseVoteCount(); //increase _votecount
};
#include "Candidate.h"
Candidate::Candidate()
{
_votecount = 0;
}
void Candidate::setVoteCount(int votecount)
{
_votecount = votecount;
}
int Candidate::getVoteCount()
{
return _votecount;
}
void Candidate::setPosition(PositionType position)
{
_position = position;
}
PositionType Candidate::getPosition()
{
return _position;
}
void Candidate::IncreaseVoteCount()
{
_votecount++;
}
I know is a super long code, and I appreciate for your patience in looking through it. My error is that it seems that it cannot recognize the function of "IncreaseVoteCount" in 'Candidate c'.
I have try to double check the syntax and the code for multiple times but i still don't understand what is the error here.
#pragma once
#include "Entity.h"
#include <iostream>
class Projectile : public Entity
{
public:
Projectile() {}
Projectile(float x, float y) {
load("Graphics/Projectile.png");
m_sprite.setPosition(x, y);
m_speed = 400;
}
};
#pragma once
#include "Entity.h"
#include <iostream>
#include "Projectile.h"
enum class Color {
red,
yellow,
brown,
blue
};
class Enemy : public Entity
{
public:
Enemy(const Color& c, const sf::Vector2f& pos) {
switch (c) {
case Color::blue:
{
load("Graphics/blueEnemy.png");
}
break;
case Color::red:
{
load("Graphics/redEnemy.png");
}
break;
case Color::yellow:
{
load("Graphics/yellowEnemy.png");
}
break;
case Color::brown:
{
load("Graphics/brownEnemy.png");
}
break;
}
setPos(pos);
m_speed = 100;
}
};
#pragma once
#include "Entity.h"
#include "Projectile.h"
#include <vector>
class Spaceship : public Entity
{
public:
Spaceship();
void move(float);
void shoot(float);
void update(float);
void draw(sf::RenderWindow*) override;
private:
bool wasSpacePressed;
std::vector<std::unique_ptr<Projectile>> m_projectiles;
sf::Clock m_clock;
};
class EnemyFleet : public Entity
{
public:
EnemyFleet();
~EnemyFleet();
void move(float);
bool isEnemyBottom() const;
bool isLeftMost() const;
bool isRightMost() const;
void moveX(float);
void moveDown();
void update(float);
void draw(sf::RenderWindow*) override;
private:
std::vector<std::unique_ptr<Enemy>> m_enemyFleet;
bool m_leftToRight; //motion of the enemyfleet
float m_speedModifier;
};
I want to be able to delete a projectile and enemy when they collide with each other, but I'm not sure how to do this since unique_ptr cannot be copied into any parameter in some collision manager class bc it has exclusive ownership. Is unique_ptr still something i should use bc i dont have to call delete (like if its a raw ptr)?
Yes, you can't copy unique_ptr, but you can copy or get reference from object that unique_ptr points to (Projectile and Enemy).
//Example
bool collision_manager::is_collision(Projectile & pr, Enemy & en) {
if (pr.position() == en.position()) {
return true;
}
return false
}
// similar for other objects that contain vector of unique_ptr
Projectile & Spaceship::get_Projectile(size_t num) {
if(num < m_projectiles.length()) {
return *projectiles.at(num);
}
throw std::invalid_argument("some alert info");
}
Instead of copying you can also use std::move(unique_ptr...).