Jump to content
  • Advertisement
Sign in to follow this  
JWColeman

Isn't it possible to use the - operator in a constructor argument?

This topic is 3183 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Plane left(-100,0);
Because that bit of code right there gives me this error:
c:\users\jonathan\documents\nehegl\logic.h(14) : error C2059: syntax error : '-'
Here is the structure I'm trying to create:
struct Plane
{
	Plane(int x, int y)
	{
		position.x = x;
		position.y = y;
		normal = position.normal();
	}
	Vector2 position;
	Vector2 normal;
};
I could be wrong, but last time I checked you could use operators in arguments?

Share this post


Link to post
Share on other sites
Advertisement
I'm just trying to initialize these planes so I can test my plane intersection function:

class Logic
{
public:
Logic();
Plane left(-100,0);
void move_paddle(Vector2 direction, Paddle & gamePaddle);
void check_collision();
private:
bool collision2d(Vector2 & a_min, Vector2 & a_max, Vector2 & b_min, Vector2 & b_max);
bool planeCollision2d(Plane & boundingPlane, Vector2 & objectPosition);
void check_paddle_collision();
void reverse_y_velocity(Vector2 & velocity);
void reverse_x_velocity(Vector2 & velocity);
Ball * ball;
Player * player1;
Player * player2;

};

Share this post


Link to post
Share on other sites
Are you doing anything that is listed here :

linky

Seeing more code would help too :)


You can't init a member inside the class declaration you need to do it at the class' constructor initializer list.


class Logic

{

public:

Logic():left(-100,0) {}

Plane left;

void move_paddle(Vector2 direction, Paddle & gamePaddle);

void check_collision();

private:

bool collision2d(Vector2 & a_min, Vector2 & a_max, Vector2 & b_min, Vector2 & b_max);

bool planeCollision2d(Plane & boundingPlane, Vector2 & objectPosition);

void check_paddle_collision();

void reverse_y_velocity(Vector2 & velocity);

void reverse_x_velocity(Vector2 & velocity);

Ball * ball;

Player * player1;

Player * player2;



};

Share this post


Link to post
Share on other sites

#ifndef PLANE_H
#define PLANE_H
#include "Vector2.h"

struct Plane
{
Plane(int x, int y)
{
position.x = x;
position.y = y;
normal = position.normal();
}
Vector2 position;
Vector2 normal;
};

#endif


And

#ifndef LOGIC_H
#define LOGIC_H
#include "Paddle.h"
#include "Ball.h"
#include "Vector2.h"
#include "Ray.h"
#include "Plane.h"
#include "Player.h"

class Logic
{
public:
Logic();
Plane left(-100,0);
void move_paddle(Vector2 direction, Paddle & gamePaddle);
void check_collision();
private:
bool collision2d(Vector2 & a_min, Vector2 & a_max, Vector2 & b_min, Vector2 & b_max);
bool planeCollision2d(Plane & boundingPlane, Vector2 & objectPosition);
void check_paddle_collision();
void reverse_y_velocity(Vector2 & velocity);
void reverse_x_velocity(Vector2 & velocity);
Ball * ball;
Player * player1;
Player * player2;

};
#endif

Share this post


Link to post
Share on other sites
Quote:
Original post by Black Knight
Are you doing anything that is listed here :

linky

Seeing more code would help too :)


You can't init a member inside the class declaration you need to do it at the class' constructor initializer list.

*** Source Snippet Removed ***


Also, if I want to do more than one, do I use another : to separate them? Or something else? Because it gives me an error if I do this:

	Logic():left(-100,0):right(100,0):top(0,100):bottom(0,-100)
{
player1 = new Player;
player2 = new Player;
ball = new Ball;
}


Sorry I'm a noob at this syntax, never used this feature before, surprisingly lol

Share this post


Link to post
Share on other sites
IT WORKS!!!

void Logic::check_collision()
{
if(ball->getVelocity().x > 0)
{
if (collision2d(ball->getMinimum(), ball->getMaximum(), player2->paddle->getMinimum(), player2->paddle->getMaximum()))
{
reverse_x_velocity(ball->getVelocity());
}
else if(planeCollision2d(right, ball->getLocation()))
{
reverse_x_velocity(ball->getVelocity());
ball->getLocation().x = 0;
ball->getLocation().y = 0;
player1->goal();
}
}
if(ball->getVelocity().x < 0)
{

if (collision2d(ball->getMinimum(), ball->getMaximum(), player1->paddle->getMinimum(), player1->paddle->getMaximum()))
{
reverse_x_velocity(ball->getVelocity());
}
else if(planeCollision2d(left, ball->getLocation()))
{
reverse_x_velocity(ball->getVelocity());
ball->getLocation().x = 0;
ball->getLocation().y = 0;
player2->goal();
}
}
if(ball->getVelocity().y > 0)
{
if (planeCollision2d(top, ball->getLocation()))
{
reverse_y_velocity(ball->getVelocity());
}
}
if(ball->getVelocity().y < 0)
{
if (planeCollision2d(bottom, ball->getLocation()))
{
reverse_y_velocity(ball->getVelocity());
}
}
}

Share this post


Link to post
Share on other sites
Also you are allocating dynamically but I don't see your destructor so that memory is not freed.

You should have a destructor that deletes these guys
player1 = new Player;
player2 = new Player;
ball = new Ball;


Logic::~Logic()
{
delete player1;
delete player2;
delete ball;
}

Though player and ball being a member of logic doesn't make much sense :)

You should also watch out for memory leaks that might occur in your constructor.The thing is in C++ when a class throws an exception from the constructor its destructor is never called.So imagine what happens when
player1 = new Player;
player2 = new Player;

allocate memory and then new Ball;(constructor of Ball) throws an exception.This means ~Logic will not be called and you won't be able to delete player1 and player2.

Here is an example that shows this :



#include <iostream>
#include <string>
#include <exception>
#include <memory>

using std::cout;
using std::endl;

class Player
{
public :
Player()
{
cout<<"Player constructor called"<<endl;
}
~Player()
{
cout<<"Player destructor called"<<endl;
}


};

class Ball
{
public :
Ball()
{
throw std::runtime_error("Ball threw exception");
cout<<"Ball constructor called"<<endl;
}
~Ball()
{
cout<<"Ball destructor called"<<endl;
}
};

class Logic
{
public:
Logic():
player1(new Player()),
player2(new Player()),
ball(new Ball())
{

}
~Logic()
{

}

Player* player1;
Player* player2;
Ball* ball;

};



int main()
{

try
{
Logic gameLogic;
}
catch(const std::runtime_error& e)
{
cout<<e.what()<<endl;
}



return 0;
}




And here is how it is fixed by using a shared_ptr



#include <iostream>
#include <string>
#include <exception>
#include <memory>

using std::cout;
using std::endl;

class Player;
class Ball;

typedef std::tr1::shared_ptr<Player> PlayerPtr;
typedef std::tr1::shared_ptr<Ball> BallPtr;

class Player
{
public :
Player()
{
cout<<"Player constructor called"<<endl;
}
~Player()
{
cout<<"Player destructor called"<<endl;
}


};

class Ball
{
public :
Ball()
{
throw std::runtime_error("Ball threw exception");
cout<<"Ball constructor called"<<endl;
}
~Ball()
{
cout<<"Ball destructor called"<<endl;
}
};

class Logic
{
public:
Logic():
player1(new Player()),
player2(new Player()),
ball(new Ball())
{

}
~Logic()
{

}

PlayerPtr player1;
PlayerPtr player2;
BallPtr ball;

};



int main()
{

try
{
Logic gameLogic;
}
catch(const std::runtime_error& e)
{
cout<<e.what()<<endl;
}



return 0;
}




To understand how this works you need to read on stack unwinding and what happens when an exception is thrown.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!