What did I do wrong C++ Error - c++

I am trying to do below program and getting following error. please tell me where I am wrong. "main.cpp(289) : error C2535: 'void __thiscall Line::setdefaultvalues(void)' : member function already defined or declare"
#include<iostream>
#define PI 3.14
#include<conio.h>
using namespace std;
class Point
{
int x;
int y;
public:
Point(int=0,int=0);
int getX();
int getY();
void setX(int);
void setY(int);
};
Point::Point(int ax,int ay){
x = ax;
y = ay;
}
int Point::getX(){
return x;
}
int Point::getY(){
return y;
}
void Point::setX(int ax){
x = ax;
}
void Point::setY( int ay){
y =ay;
}
class Shape
{
public:
virtual void draw()
{
cout<<"draw a shape"<<endl;
}
virtual int calcarea()
{
cout<<"area of shape is"<<endl;
return 0;
}
};
class Rectangle:public Shape
{
Point p1;
Point p2;
Point p3;
Point p4;
public:
Rectangle(Point &a ,Point &b ,Point &c ,Point &d);
Point getp1();
Point getp2();
Point getp3();
Point getp4();
virtual void draw();
virtual int calcarea();
int getlength();
int getWidth();
void show()
{
cout<<"P1 ( "<<p1.getX()<<","<<p1.getY()<<" )"<<endl;
cout<<"P2 ( "<<p2.getX()<<","<<p2.getY()<<" )"<<endl;
cout<<"P3( "<<p3.getX()<<","<<p3.getY()<<" )"<<endl;
cout<<"P4( "<<p4.getX()<<","<<p4.getY()<<" )"<<endl;
//p3.setX(8);
// p3.setY(4);
//p4.setX(0);
//p4.setY(4);
}
void defaultvalues (){
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(0);
p3.setX(8);
p3.setY(4);
p4.setX(0);
p4.setY(4);
}
};
Rectangle::Rectangle(Point &a,Point &b,Point &c,Point &d){
if ((a.getX()<0||a.getX()>20)||(a.getY()<0||a.getY()>20)||
(b.getX()<0||b.getX()>20) ||(b.getY()<0||b.getY()>20)||
( c.getX()<0||c.getX()>20) ||(c.getY()<0||c.getY()>20)||
(d.getX()<0||d.getX()>20) ||(d.getY()<0||d.getY()>20))
{
std::cout << "Incorect Point value for constructor replacing with default values" <<std::endl;
defaultvalues();
}
else
{
p1.setX(a.getX());
p1.setY(a.getY());
p2.setX(b.getX());
p2.setY(b.getY());
p3.setX(c.getX());
p3.setY(c.getY());
p4.setX(d.getX());
p4.setY(d.getY());
}
/* if( a.getX() != c.getX() )
cout<<"error:a and c must have same x values"<<endl;
if( b.getX() != d.getX() )
cout<<"error:b and c must have same x values"<<endl;
*/
}
int Rectangle ::getlength(){
int l;
if(p1.getX()==p2.getX())
{
l=p2.getY()-p1.getY();
}
else if(p2.getX()==p3.getX())
{
l=p3.getY()-p1.getY();
}
else if(p1.getX()==p4.getX())
{
l=p4.getY()-p1.getY();
}
else
{
l=0.0;
}
int w;
if(p1.getY()==p2.getY())
{
w=p2.getX()-p1.getX();
}
else if(p2.getY()==p3.getY())
{
w=p3.getX()-p1.getX();
}
else if(p1.getY()==p4.getY())
{
w=p4.getX()-p1.getX();
}
else
{
w=0.0;
}
if (l>w)
{
return l;
}
else
{
return w;
}
};
int Rectangle::getWidth()
{
int l;
int w;
if(p1.getX()==p2.getX())
{
l=p2.getY()-p1.getY();
}
else if(p2.getX()==p3.getX())
{
l=p3.getY()-p1.getY();
}
else if(p1.getX()==p4.getX())
{
l=p4.getY()-p1.getY();
}
else
{
l=0.0;
}
if(p1.getY()==p2.getY())
{
w=p2.getX()-p1.getX();
}
else if(p2.getY()==p3.getY())
{
w=p3.getX()-p1.getX();
}
else if(p1.getY()==p4.getY())
{
w=p4.getX()-p1.getX();
}
else
{
w=0.0;
}
if (l<w)
{
return l;
}
else
{
return w;
}
}
int Rectangle::calcarea()
{
int l=getlength();
int w=getWidth();
return l*w;
}
void Rectangle:: draw()
{
cout<<"this is a rectangle"<<endl;
}
class Line : public Shape {
Point p1;
Point p2;
public:
void setdefaultvalues();
Line(Point&a,Point&b)
{
if((a.getX()<0||a.getX()>20)
||(a.getY()<0||a.getY()>20)
||(b.getX()<0||b.getX()>20)
||(b.getY()<0||b.getY()>20))
{
setdefaultvalues();
}
else
{
if((a.getX()==b.getX()||a.getY()==b.getY()))
{
setdefaultvalues();
}
else
{
p1.setX(a.getX());
p1.setY(a.getY());
p2.setX(b.getX());
p2.setY(b.getY());
}
}
}
void setdefaultvalues()
{
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(8);
}
virtual void draw()
{
cout<<"this is a line"<<endl;
}
void show()
{
cout<<"P1 ( "<<p1.getX()<<","<<p1.getY()<<" )"<<endl;
cout<<"P2 ( "<<p2.getX()<<","<<p2.getY()<<" )"<<endl;
}
};
/*class Circle : public Shape {
Point centre;
double radius;
public:
Circle(Point&a, double r)
{
if(r>0&&r<20)
{
radius=r;
centre.setX(a.getX());
centre.setY(a.getY());
}
else
{
radius=3;
centre.setX(0);
centre.setY(0);
}
}
virtual void draw()
{
cout<<"this is a circle"<<endl;
}
virtual int calcarea()
{
cout<<PI*radius*radius<<endl;
}
};*/
class Triangle:public Shape{
Point p1;
Point p2;
Point p3;
public:
Triangle(Point&a,Point&b,Point&c){
if((a.getX()==b.getX())&&(a.getY()==b.getY())
&&(a.getX()==c.getX())&&(a.getY()==c.getY())
&&(b.getX()==c.getX())&&(b.getY()==c.getY()))
{
std::cout << "Incorect values" <<std::endl;
}
else
{ p1.setX(15);
p1.setY(15);
p2.setX(23);
p2.setY(30);
p3.setX(50);
p3.setY(25);
}
virtual int calcarea();
virtual void draw();
}
virtual int calcarea()
{ int area;
area=[p1*getX()(p2*getY()-p3*getY())+p2*getX()(p3*getY()-p1*getY())+p3*getX()(p1*getY()-p2*getY())]/2;
return area;
}
};
void drawshapes();
int main() {
Point p1(0,0),p2(0,4),p3(4,0),p4(4,4);
Shape* _shape[ 4 ];
_shape[0]=new Rectangle(p1,p2,p3,p4);
/*drawshapes(_shape,4);
calarea(_shape,4);
*/
Line l1(p1,p2),l2(p1,p3);
//_shape[0]=new circle(p1,3);
_shape[1]=new Line(p1,p4);
_shape[2]=new Line(p2,p3);
/* drawshapes(_s,4);
calcarea(_s,4);*/
return 0;
}

You have this code:
class Line : public Shape
{
Point p1;
Point p2;
public:
void setdefaultvalues();
...
void setdefaultvalues()
{
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(8);
}
...
}
Turn that code into this:
class Line : public Shape
{
Point p1;
Point p2;
public:
void setdefaultvalues()
{
p1.setX(0);
p1.setY(0);
p2.setX(8);
p2.setY(8);
}
...
}
In other words take the two declarations of the setdefaultvalues function and reduce it to one.
EDIT: Changed definition to declaration.

Related

How do I instantiate a class from a member variable pointer pointing to the class in c++?

So I have two classes "PlayerTracking" and "Projectile". Every instance of player tracking should have an associated projectile; the PlayerTracking class has a pointer to a projectile to connect the two. When I call the "getPlayerProjectile" function I would like the constructor in the Projectile class to initialise the projectile but I don't really have any idea what is the best way to go about doing this. Here is the player tracking.h file
#pragma once
#include"GameObject.h"
#include"Projectile.h"
class playerTracking :public GameObject {
public:
playerTracking() {
playerYawOrientation = 0.0f;
playerPitchOrientation = 0.0f;
playerID = 1;
teamID = 0;
IndividualplayerScore = 0;
playerProjectile = new Projectile();
}
void setplayerID(int assignedPlayerID) {
playerID = assignedPlayerID;
}
void setTeamID(int assignedTeamID) {
playerID = assignedTeamID;
}
void AssignPlayerWeapon(gun weponType) {
playerProjectile->setGunType(weponType);
}
Projectile* getPlayerProjectile() {
return playerProjectile;
}
protected:
float playerYawOrientation;
float playerPitchOrientation;
int playerID;
int teamID;
int IndividualplayerScore;
Projectile* playerProjectile;
};
and here is the projectile class.h
#pragma once
#include "Transform.h"
#include "CollisionVolume.h"
#include"GameObject.h"
struct gun {
float radius;
float projectileForce;
float weight;
bool affectedByGravity;
};
static gun pistol{
7,
50,
1,
false,
};
static gun rocket{
20,
20,
5,
true,
};
class NetworkObject;
class RenderObject;
class PhysicsObject;
class Projectile:public GameObject {
public:
/* Projectile() {
setGunType(pistol);
aimingPitch = 0.0f;
aimingYaw = 0.0f;
physicsProjectile = nullptr;
}*/
Projectile();
~Projectile() {
physicsProjectile;
}
void setGunType(gun wepType);
void setExplosionRadius(float newRadius) {
explosionRadius = newRadius;
}
float getExplosionRadius() {
return explosionRadius;
}
void setProjectilePropultionForce(float newForce) {
projectilePropultionForce = newForce;
}
float getPojectilePropultionForce() {
return projectilePropultionForce;
}
void setWieght(float newweight) {
weight = newweight;
}
float getWeight() {
return weight;
}
void setAffectedByGravityTrue() {
AffectedGravity = true;
}
void setAffectedByGravityFalse() {
AffectedGravity = false;
}
void setBulletDirectionVector(Vector3 aimedDirection) {
bulletDirectionVector = aimedDirection.Normalised();
}
Vector3 getBulletDirectionVector() {
return bulletDirectionVector;
}
bool getAffectedGravity() {
return AffectedGravity;
}
protected:
float explosionRadius;
float projectilePropultionForce;
float weight;
float aimingYaw;
float aimingPitch;
//static int TeamID;
//static int personalID;
bool AffectedGravity;
PhysicsObject* physicsProjectile;
Vector3 bulletDirectionVector;
};
and the Projectile.c file
#include"Projectile.h"
Projectile::Projectile(){
setGunType(pistol);
aimingPitch = 0.0f;
aimingYaw = 0.0f;
physicsProjectile = nullptr;
bulletDirectionVector = { 0,0,0 };
}
void Projectile::setGunType(gun wepType) {
setExplosionRadius(wepType.radius);
setProjectilePropultionForce(wepType.projectileForce);
setWieght(wepType.weight);
//SetBoundingVolume(VolumeType::Sphere);
if (wepType.affectedByGravity) {
setAffectedByGravityTrue();
}
else
{
setAffectedByGravityFalse();
}
}
Obviously right now the pointer playerProjectile is just null but I am pretty sure using new would be a bad way to construct one in this case. Thanks a bunch for any help!

SFML setFillColor doesn't work to a class member

I think I should write implementation for my Circle class, but I'm not sure and I don't know, how to transfer Color as function parameter in main bcz compiler doesn't work with sf::Color::Red or just Red as function parameter in main function
#include <SFML/Graphics.hpp>
using namespace sf;
const int APPLICATION_WIDTH = 400;
const int APPLICATION_HEIGHT = 300;
class Figure : public sf::Shape{
protected:
double m_x0 = 0, m_y0 = 0;
float m_angle = 0;
double m_scale;
public:
virtual ~Figure() {}
void setCoordX(double x0) { m_x0 = x0; }
void setCoordY(double y0) { m_y0 = y0; }
void setAngle(float angle) { m_angle = angle; }
void setScale(double scale) { m_scale = scale; }
double getCoordX() { return m_x0; }
double getCoordY() { return m_y0; }
float getAngle() { return m_angle; }
double getScale() { return m_scale; }
virtual void drawFigure(sf::RenderWindow& w) = 0;
//virtual void moveFigure(sf::RenderWindow& w, const double vx, const double vy) = 0;
void hideFigure(sf::RenderWindow& w);
virtual void rotateFigure(sf::RenderWindow& w) = 0;
//virtual void scaleFigure(const double vx, const double vy) = 0;
};
void Figure::rotateFigure(sf::RenderWindow& w) {
sf::Shape::rotate(m_angle);
}
void Figure::hideFigure(sf::RenderWindow& w) {
sf::Shape::setFillColor(sf::Color::Transparent);
}
class Circle : public Figure {
private:
CircleShape m_obj;
//double m_x1 , m_y1;
double m_radius = 1;
Vector2f getPoint(std::size_t index) const override {
return m_obj.getPoint(index);
}
std::size_t getPointCount() const override {
return m_obj.getPointCount();
}
public:
//void setCoordX1(double x1) { m_x1 = x1; }
void setRad(double r) { m_radius = r; }
double getRad() { return m_obj.getRadius(); }
double getCenterX() { return m_obj.getRadius(); }
double getCenterY() { return m_obj.getRadius(); }
void drawFigure(sf::RenderWindow& w);
//void moveFigure(sf::RenderWindow& w, const double vx, const double vy);
//void hideFigure(sf::RenderWindow& w) override;
void rotateFigure(sf::RenderWindow& w) override;
};
void Circle::drawFigure(sf::RenderWindow& w) {
m_obj.setRadius(m_radius);
m_obj.setPosition(m_x0, m_y0);
w.draw(m_obj);
}
//void Circle::hideFigure(sf::RenderWindow &w) {
// m_obj.setFillColor(sf::Color::Transparent);
//}
void Circle::rotateFigure(sf::RenderWindow& w) {
//m_obj.setFillColor(sf::Color::Magenta); // if I'll paint it here, it works
m_obj.rotate(m_angle);
}
int main()
{
RenderWindow window(VideoMode(APPLICATION_WIDTH, APPLICATION_HEIGHT), "Lab 6 using SFML");
Circle a, b, c, d;
a.setFillColor(sf::Color::Red);
b.setFillColor(sf::Color::Green);
//c.setFillColor(sf::Color::Blue);
//d.setFillColor(sf::Color::Magenta);
a.setRad(32);
a.setCoordX(50); a.setCoordY(34);
b.setRad(16);
b.setCoordX(10); b.setCoordY(34);
b.setAngle(45);
b.rotateFigure(window);
//b.hideFigure(window);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
//sf::CircleShape circle;
//circle.setRadius(50);
//circle.setFillColor(sf::Color::Green);
//circle.setPosition((APPLICATION_WIDTH - (circle.getRadius()*2))/2.0,
// (APPLICATION_HEIGHT - (circle.getRadius()*2))/2.0);
//circle.move(30, 30);
//window.draw(circle);
a.drawFigure(window);
b.drawFigure(window);
window.display();
}
return 0;
}
sf::Color is a class that can be passed as a parameter. You can read how sf::Color works in the documentation: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Color.php
Example:
#include <SFML/Graphics.hpp>
class MyCircle
{
public:
MyCircle()
{
mCircle.setRadius(50.f);
}
void setColor(sf::Color color)
{
mCircle.setFillColor(color);
}
void draw(sf::RenderWindow& window)
{
window.draw(mCircle);
}
private:
sf::CircleShape mCircle;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
MyCircle circle;
circle.setColor(sf::Color::Red);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
circle.draw(window);
window.display();
}
return 0;
}

'Read access violation' exception in an SFML/C++ project:

I'm recently learning to make a 2d game in SFML using a tutorial series on youtube by Suraj Sharma(Video 57):
https://www.youtube.com/watch?v=kwd_AVCkvXE&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=57
His Source Code:
https://github.com/Headturna/SFML_RPG
Right now he's teaching me to symplify the game's menu system by making a mini class 'StateData' in the parent class 'State' so any inherited class can access 'State' parameters via 'StateData'(Ex:MainMenu(StData* Stdata){}).
After debugging the game seems to runs fine.But everytime i click on something on the menu(start,settings,etc...) a 'Read access violation:Stdata was nullptr' occurs in the 'State' class constructor.
Here's the code:
State.h:
#pragma once
#ifndef STATE_H
#define STATE_H
#include "Player.h"
#include "GrphSettings.h"
class Player;
class GrphSettings;
class State;
class StData {
public:
StData(){}
//Vars
float GridSize;
sf::RenderWindow* Window;
GrphSettings* GSettings;
std::map<std::string, int>* SupportedKeys;
std::stack<State*>* states;
};
class State
{
private:
protected:
StData* Stdata;
std::stack<State*>* states;
sf::RenderWindow* window;
std::map<std::string, int>* SupportedKeys ;
std::map<std::string, int> Keybinds;
bool quit;
bool pause;
float keyTime;
float keyTimeMax;
float GridSize;
sf::Vector2i MousePosScr;
sf::Vector2i MousePosWind;
sf::Vector2f MousePosView;
//Resources
std::map<std::string,sf::Texture> texture;
//Funcs
virtual void InitKeybinds() = 0;
public:
State(StData* Stdata);
virtual~State();
//Access
const bool getKeytime();
const bool& getquit()const;
//Funcs
void Endstate();
void PauseSt();
void UnPauseSt();
virtual void UpdateInput(const float& dt) = 0;
virtual void UpdateMousePos();
virtual void UpdateKeyTime(const float& dt);
virtual void Update(const float& dt) = 0;
virtual void Render(sf::RenderTarget* target = nullptr) = 0;
};
#endif // !1
State.cpp:
#include "pch.h"
#include "State.h"
State::State(StData* Stdata)
{
this->Stdata = Stdata;
this->window = Stdata->Window;
this->SupportedKeys = Stdata->SupportedKeys;
this->states = Stdata->states;
this->quit = false;
this->pause = false;
this->keyTime = 0.f;
this->keyTimeMax = 10.f;
this->GridSize = Stdata->GridSize;
}
State::~State()
{
}
//Access
const bool State::getKeytime()
{
if (this->keyTime >= this->keyTimeMax) {
this->keyTime = 0.f;
return true;
}
return false;
}
const bool& State::getquit() const
{
// TODO: insert return statement here
return this->quit;
}
//Funcs
void State::Endstate()
{
this->quit = true;
}
void State::PauseSt()
{
this->pause = true;
}
void State::UnPauseSt()
{
this->pause = false;
}
void State::UpdateMousePos()
{
this->MousePosScr = sf::Mouse::getPosition();
this->MousePosWind = sf::Mouse::getPosition(*this->window);
this->MousePosView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window));
}
void State::UpdateKeyTime(const float& dt)
{
if (this->keyTime < this->keyTimeMax)
this->keyTime += 100.f * dt;
}
Every button one the menu(start,exit...)is an inherited state from 'State' class.This is,for example,the 'Edit' state.
Edit.h:
#pragma once
#ifndef EDIT_H
#define EDIT_H
#include "State.h"
#include "Gui.h"
#include "PauseMenu.h"
#include "TileMap.h"
class State;
class Gui;
class PauseMenu;
class TileMap;
class Edit:public State
{
private:
//Vars
sf::Font Fnt;
PauseMenu* PMenu;
std::map<std::string, gui::Button*> buttons;
TileMap Map;
//Functions
void InitVars();
void InitBackGrnd();
void InitFonts();
void InitKeybinds();
void InitPauseMenu();
void InitBtn();
public:
Edit(StData* Stdata);
virtual~Edit();
//Functions
void UpdateInput(const float& dt);
void Update(const float& dt);
void UpdatePButtons();
void UpdateBtn();
void Endstate();
void RenderBtn(sf::RenderTarget& target);
void Render(sf::RenderTarget* target = nullptr);
};
#endif // ! EDIT_H
Edit.cpp:
#include "pch.h"
#include "Edit.h"
void Edit::InitVars()
{
}
void Edit::InitBackGrnd()
{
}
void Edit::InitFonts()
{
if (!this->Fnt.loadFromFile("Fonts/SPACEMAN.ttf")) {
throw("Error::Edit::Couldn't load font");
}
}
void Edit::InitKeybinds()
{
std::ifstream ifs("Config/EditKeys.ini");
if (ifs.is_open()) {
std::string key = "";
std::string key2 = "";
int keyval = 0;
while (ifs >> key >> key2)
{
this->Keybinds[key] = this->SupportedKeys->at(key2);
}
}
ifs.close();
this->Keybinds["Close"] = this->SupportedKeys->at("ESC");
this->Keybinds["Left"] = this->SupportedKeys->at("A");
this->Keybinds["Right"] = this->SupportedKeys->at("D");
this->Keybinds["Up"] = this->SupportedKeys->at("W");
this->Keybinds["Down"] = this->SupportedKeys->at("S");
}
void Edit::InitPauseMenu()
{
this->PMenu = new PauseMenu(*this->window, this->Fnt);
this->PMenu->addButtons("Quit", 800.f, "Quit");
}
void Edit::InitBtn()
{
}
Edit::Edit(StData* Stdata)
:State(Stdata)
{
this->InitVars();
this->InitBackGrnd();
this->InitFonts();
this->InitKeybinds();
this->InitPauseMenu();
this->InitBtn();
}
Edit::~Edit()
{
auto it = this->buttons.begin();
for (it = this->buttons.begin(); it != this->buttons.end(); ++it) {
delete it->second;
}
delete this->PMenu;
}
//Funcs
void Edit::UpdateInput(const float& dt)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->Keybinds.at("Close")))
&& this->getKeytime()) {
if (!this->pause)
this->PauseSt();
else this->UnPauseSt();
}
}
void Edit::Update(const float& dt)
{
this->UpdateMousePos();
this->UpdateKeyTime(dt);
this->UpdateInput(dt);
if (!this->pause) {//Unpaused
this->UpdateBtn();
}
else {//Paused
this->PMenu->Update(this->MousePosView);
this->UpdatePButtons();
}
this->UpdateBtn();
std::cout << this->MousePosView.x << " " << this->MousePosView.y << "\r";
}
void Edit::UpdatePButtons()
{
if (this->PMenu->isPressed("Quit"))
this->Endstate();
}
void Edit::UpdateBtn()
{ //Update buttons and handle their functions
for (auto& it : this->buttons) {
it.second->Update(MousePosView);
}
}
void Edit::Endstate()
{
std::cout << "Ending State" << "\n";
}
void Edit::RenderBtn(sf::RenderTarget& target)
{
for (auto& it : this->buttons) {
it.second->Render(target);
}
}
void Edit::Render(sf::RenderTarget* target)
{
if (!target)
target = this->window;
this->RenderBtn(*target);
this->Map.Render(*target);
if (this->pause) {//Pause Menu
this->PMenu->Render(*target);
}
sf::Text MText;
MText.setPosition(this->MousePosView);
MText.setFont(this->Fnt);
MText.setCharacterSize(12);
std::stringstream ss;
ss << this->MousePosView.x << ' ' << this->MousePosView.y;
MText.setString(ss.str());
target->draw(MText);
}
Can anyone help me ?

STL list push_back crashes when called from a pointer list

I have a problem that arised with another question I made. I am using a list of pointers of a custom class (class that I have implemented), and the program crashes when I call this method, however, if I do not use a pointer list, it works fine. This is the function where the program crashes:
void enemy_spawner()
{
Point2D enemy_pos = Point2D(rand() % (SCREEN_WIDTH - 20) + 20, -20);
Point2D enemy_vel = Point2D(0, rand() % 4 + 1);
Sprite* enemy = new Sprite(enemy_pos, enemy_vel, enemy_image, enemy_info);
//Kamikaze* enemy = new Kamikaze(enemy_pos, enemy_vel, 0, enemy_image, enemy_info);
if (enemy_group.size() < MAX_ENEMIES)
{
// Program crashes here
enemy_group.push_back(enemy);
}
}
The enemy_group is a global variable defined as follows:
std::list<Sprite*> enemy_group;
My Sprite class is as follows (I have read that I need to have properly defined copy constructors, etc)
class Sprite
{
private:
Point2D sp_pos;
Point2D sp_vel;
SDL_Surface* sp_img;
Point2D sp_center;
Point2D sp_size;
double sp_radius;
bool sp_animated;
int sp_frames;
int sp_cur_frame;
public:
Sprite() {}
Sprite(Point2D pos, Point2D vel, SDL_Surface *img, ImageInfo info, bool animated = false, int frames = 0);
virtual void draw(SDL_Surface* screen);
virtual void update();
void setInfo (ImageInfo info);
void setPos( Point2D pos ) { sp_pos = pos; }
void setVel( Point2D vel ) { sp_vel = vel; }
void setImg (SDL_Surface* img) { sp_img = img; }
void setNextFrame() { sp_cur_frame++; }
void setFrame( int frame ) { sp_cur_frame = frame; }
void setAnimated(bool animated) { sp_animated = animated; }
void changeVelX (int c) { sp_vel.setX(c);}
void changeVelY (int c) { sp_vel.setY(c);}
void changePosX (int c) { sp_pos.setX(c);}
void changePosY (int c) { sp_pos.setY(c);}
SDL_Surface* getImg() { return sp_img; }
Point2D getPos() { return sp_pos; }
Point2D getVel() { return sp_vel; }
Point2D getCenter() { return sp_center; }
Point2D getSize() { return sp_size; }
double getRadius() { return sp_radius; }
int getCurFrame() { return sp_cur_frame; }
int getFrames() { return sp_frames; }
bool collide(Sprite &another_sprite);
virtual ~Sprite() {}
Sprite& operator=(Sprite other) {
std::swap(sp_pos, other.sp_pos);
std::swap(sp_vel, other.sp_vel);
std::swap(sp_img, other.sp_img);
std::swap(sp_center, other.sp_center);
std::swap(sp_size, other.sp_size);
std::swap(sp_radius, other.sp_radius);
std::swap(sp_animated, other.sp_animated);
std::swap(sp_frames, other.sp_frames);
std::swap(sp_cur_frame, other.sp_cur_frame);
return *this;
}
Sprite(const Sprite &obj)
{
sp_pos = obj.sp_pos;
sp_vel = obj.sp_vel;
sp_img = new SDL_Surface;
*sp_img = *obj.sp_img;
sp_center = obj.sp_center;
sp_size = obj.sp_size;
sp_radius = obj.sp_radius;
sp_animated = obj.sp_animated;
sp_frames = obj.sp_frames;
sp_cur_frame = obj.sp_cur_frame;
}
};
If it matters, I am using gcc compiler (Actually my IDE is codeblocks). The question is, why does the program crash? How do I solve this issue?
Thanks in advance

Implementation of Polygon class

I am writing a program that contains three classes: They are point, line and polygon. I have written the first two, but I am having trouble with the last one.
This class has to have two constructors, one of them builds an object with a point (tip of polygon) and another one builds it with a line. These functions have to be as follows:
polygon(point** arr,int size) and polygon(line** arr,int size).
I don't know why point and line are pointer to pointer? What are the attributes of the polygon class and how can I write polygon's constructor?
class point
{
private:
int first;
int second;
public:
point(void);
point (int x,int y);
point (const point& other);
int getX();
int getY();
int distance(point* other);//distance of two point
line* Line(point*);//build a line with two point
polygon* triangle(point*,point*);//build a triangle with three point
~point(void);
};
point::point(void)
{
first=0;
second=0;
}
point::point (int x,int y)
{
first=x;
second=y;
}
point::point(const point& other)
{
first=other.first;
second=other.second;
}
int point::gha(int a)
{
if(a>=0)
return a;
else
return -a;
}
int point::pow(int a)
{
return a*a;
}
int point::getX(){return first;}
int point::getY(){return second;}
int point::distance(point* other)
{
int d= sqrt((pow(first-other->first))+(pow(second-other->second)));
return d;
}
line* point::Line(point* other)
{
line l(this,other);
return &l;
}
polygon* point::triangle(point*,point*){
}
point::~point(void)
{
}
////////////////////////////
class line
{
private:
int m;
int c;
public:
line(void);
line(point*,point*);
line(int ,point*);
bool isParallel(line*);
bool isPrependicular(line*);
point* intersection(line*);
line* parallel(point*);
polygon* triangle(line*,line*);
~line(void);
};
line::line(void)
{
m=1;
c=0;
}
line::line(point* a,point* b)
{
m=((a->getY())-b->getY())/(a->getX()-b->getX());
c=a->getY()-(m*(a->getX()));
}
line::line(int dip,point* a)
{
m=dip;
c=a->getY()-(dip*(a->getX()));
}
bool line:: isParallel(line* other)
{
if(m==other->m)
return true;
else
return false;
}
bool line::isPrependicular(line* other)
{
if((m*other->m)==1 || (m*other->m)==-1)
return true;
else
return false;
}
point* line::intersection(line* other)
{
int x=(other->c-c)/(m-other->m);
int y=(m*x)+c;
point p (x,y);
return &p;
}
line* line::parallel(point* other)
{
line l(m,other);
return &l;
}
line::~line(void)
{
}
///////////////////////////////////
class polygon
{
private:
int count;
point* tip;
line* l;
public:
polygon(void);
polygon(point** arr,int size);
polygon(line** arr,int size);
bool isTriangle();
bool isSquare();
~polygon(void);
};