C++ class pointer giving error - c++

In my program I have created an Object class, that's not overly complicated (has a lot of types of constructors but that's beside the point). I have looked at the syntax for creating a pointer to a class, and I can't seem to find a problem with it, but it keeps producing this error code
Error: a value of type "Object (*)()" cannot be used to initialize an entity of type "Object
*"
System Info: Windows Embedded 8.1 x64
IDE: Microsoft Visual Studio 2013 Update 4
Object.h:
#ifndef OBJECT_H
#define OBJECT_H
#include "Point.h"
class Object {
private:
float mf_Mass;
float mf_Volume;
int mn_ID;
bool mb_Parent;
bool mb_Child;
Point position;
Object* mp_Child;
Object* mp_Parent;
public:
//ID generation
static int c_IDGenerator;
void generateID(){ mn_ID = c_IDGenerator++; return; }
//Constructor Prototypes
Object();
Object(Point point);
Object(Object* child);
Object(float mass);
Object(Point point, Object* child);
Object(Point point, float mass);
Object(Object* child, Object* parent);
Object(Object* child, float mass);
Object(float mass, float volume);
Object(Point point, Object* child, Object* parent);
Object(Point point, Object* child, float mass);
Object(Object* child, Object* parent, float mass);
Object(Object* child, float mass, float volume);
Object(Point point, Object* child, Object* parent, float mass);
Object(Point point, Object* child, float mass, float volume);
Object(Object* child, Object* parent, float mass, float volume);
Object(Point point, Object* child, Object* parent, float mass, float volume);
//Get and Set methods
float getMass(){ return mf_Mass; }
float getVolume(){ return mf_Volume; }
int getID(){ return mn_ID; }
Point getPosition(){ return position; }
Object& getChild(){ return &(mp_Child); }
Object& getParent(){ return &(mp_Parent); }
bool isParent(){ return mb_Parent; }
bool isChild(){ return mb_Child; }
void setMass(float mass){ mf_Mass = mass; return; }
void setVolume(float volume){ mf_Volume = volume; return; }
void setChild(Object* child){ mp_Child = child; return; }
void setParent(Object* parent){ mp_Parent = parent; return; }
//Object Interface
void move(int x, int y);
void move(Point point);
void destroyChild();
void resetChild();
void resetParent();
float calculateDensity();
}
#endif
Object.cpp:
#include "Object.h"
//Object Constructors with 0 known field(s)
Object::Object()
: position(0, 0)
{
mf_Mass = 1;
mf_Volume = 1;
mp_Child = 0;
mp_Parent = 0;
mb_Child = false;
mb_Parent = false;
generateID();
}
//Object Constructors with 1 known field(s)
Object::Object(Point point)
: position(point)
{
mf_Mass = 1;
mf_Volume = 1;
mp_Child = 0;
mp_Parent = 0;
mb_Child = false;
generateID();
}
Object::Object(Object* child)
: position(0, 0)
{
mf_Mass = 1;
mf_Volume = 1;
mp_Child = child;
mp_Parent = 0;
generateID();
}
Object::Object(float mass)
: position(0, 0)
{
mf_Mass = mass;
mf_Volume = 0;
mp_Child = 0;
mp_Parent = 0;
generateID();
}
//Object Constructors with 2 known field(s)
Object::Object(Point point, Object* child)
: position(point)
{
mf_Mass = 1;
mf_Volume = 1;
mp_Child = child;
mp_Parent = 0;
generateID();
}
Object::Object(Point point, float mass)
: position(point)
{
mf_Mass = mass;
mf_Volume = 1;
mp_Child = 0;
mp_Parent = 0;
generateID();
}
Object::Object(Object* child, Object* parent)
: position(0, 0)
{
mf_Mass = 1;
mf_Volume = 1;
mp_Child = child;
mp_Parent = parent;
generateID();
}
Object::Object(Object* child, float mass)
: position(0, 0)
{
mf_Mass = mass;
mf_Volume = 1;
mp_Child = child;
mp_Parent = 0;
generateID();
}
Object::Object(float mass, float volume)
: position(0, 0)
{
mf_Mass = mass;
mf_Volume = volume;
mp_Child = 0;
mp_Parent = 0;
generateID();
}
//Object Constructors with 3 known field(s)
Object::Object(Point point, Object* child, Object* parent)
: position(point)
{
mf_Mass = 1;
mf_Volume = 1;
mp_Child = child;
mp_Parent = parent;
generateID();
}
Object::Object(Point point, Object* child, float mass)
: position(point)
{
mf_Mass = mass;
mf_Volume = 1;
mp_Child = child;
mp_Parent = 0;
generateID();
}
Object::Object(Object* child, Object* parent, float mass)
: position(0, 0)
{
mf_Mass = mass;
mf_Volume = 1;
mp_Child = child;
mp_Parent = parent;
generateID();
}
Object::Object(Object* child, float mass, float volume)
: position(0, 0)
{
mf_Mass = mass;
mf_Volume = volume;
mp_Child = child;
mp_Parent = 0;
generateID();
}
//Object Constructors with 4 known field(s)
Object::Object(Point point, Object* child, Object* parent, float mass)
: position(point)
{
mf_Mass = mass;
mf_Volume = 1;
mp_Child = child;
mp_Parent = parent;
generateID();
}
Object::Object(Object* child, Object* parent, float mass, float volume)
: position(0, 0)
{
mf_Mass = mass;
mf_Volume = volume;
mp_Child = child;
mp_Parent = parent;
generateID();
}
//Object Constructors with 5 known field(s)
Object::Object(Point point, Object* child, Object* parent, float mass, float volume)
: position(point)
{
mf_Mass = mass;
mf_Volume = volume;
mp_Child = child;
mp_Parent = parent;
generateID();
}
//Object Interaction Methods
void Object::move(int x = 0, int y = 0)
{
position.move(x, y);
}
void Object::move(Point point)
{
position.move(point);
}
void Object::destroyChild()
{
if(mp_Child)
{
mp_Child->~Object();
resetChild();
}
else
{
resetChild();
}
}
void Object::resetChild()
{
mp_Child = 0;
mb_Parent = false;
}
void Object::resetParent()
{
mp_Parent = 0;
mb_Child = false;
}
float Object::calculateDensity()
{
return (mf_Mass / mf_Volume);
}
Point.h:
class Point {
public:
int _X;
int _Y;
Point()
{
_X = 0;
_Y = 0;
}
Point(int x)
{
_X = x;
_Y = 0;
}
Point(int x, int y)
{
_X = x;
_Y = y;
}
void reset()
{
_X = 0;
_Y = 0;
}
void moveX(int x)
{
_X = x;
}
void moveY(int y)
{
_Y = y;
}
void move(int x, int y)
{
_X = x;
_Y = y;
}
void move(Point point)
{
_X = point._X;
_Y = point._Y;
}
void offsetX(int osX)
{
_X += osX;
}
void offsetY(int osY)
{
_Y += osY;
}
void offset(int osX, int osY)
{
_X += osX;
_Y += osY;
}
}
Main.cpp:
#include "Object.h"
#include "Point.h"
#include <iostream>
using namespace std;
int main()
{
Point origin;
Point p1(1);
Point p2(1, 3);
Object obj1();
Object* child;
child = &obj1;
Object obj2(origin);
Object obj3(&obj2);
Object obj4(1.25);
Object obj5(p1, &obj1);
Object obj6(p1, 4.0f);
Object obj7(&obj1, 4.0f);
Object obj8(&obj2, &obj3);
Object obj9(2.0f, 4.0f);
Object obj10(p2, &obj1, &obj2);
Object obj11(p2, &obj1, 4.0f);
Object obj12(&obj1, &obj2, 3.0f);
Object obj13(&obj1, 2.5f, 2.6f);
Object obj14(origin, &obj10, &obj11, 10.0f);
Object obj15(&obj12, &obj13, 11.0f, 13.0f);
Object obj16(p1, &obj14, &obj15, 100.0f, 100.0f);
Object objs[] = { obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13, obj14, obj15, obj16 };
for (int i = 0; i < 16; i++)
{
cout << objs[i];
}
system("pause");
return 0;
}
PLEASE NOTE: I know I have not overloaded the operator<< function yet, that is to come. I want to get the other problem fixed before I attempt to move on with anything.
Also, I understand that the "system("pause");" line is not recommended and frowned upon, this is just a personal project so I don't care that it is OS specific at the moment, I'll change it if I decide to port to another platform.
I can't find anything wrong with my syntax or logic but, I'm pretty new to C++ classes so I'm open for any suggestions, thanks!!!

This:
Object obj1();
is a function declaration. It is a function named obj1 that takes zero arguments and returns an Object. This is known as the "most vexing parse." What you meant was:
Object obj1;
You can tell this from the error message, indicating that your right-hand-side is of type "pointer to function that takes zero arguments and returns an Object rather than the "pointer to Object" that you were expecting:
Error: a value of type "Object (*)()" cannot be used to initialize an entity of type "Object *"

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!

Deep copy unsuccessful

might be a stupid question and if it is, let me know, I will delete it as soon as possible. The thing is I have to make a deep copy in class "Kambarys" (ignore mixed languages, I know I shouldn't do that). Program terminates after trying to call function second time. Probably the problem is my syntax in constructor copy, but I can't find the correct one anywhere. One of the requirements is to create langas, durys and kambarys in dynamic memory using "new" and delete windows vector and door in Kambarys destructor. Appreciate the help!
Requirements:
In the main method, use the new operator to create room k1, add windows and doors to it. Write a constructor Room (const Room & k) that would create a correct copy. In the method main, write another room k2. Calculate the length of the baseboards / wall area.
Perform the following steps: k2 = * k1; delete k1;
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
class Langas{
private:
float height;
float widht;
static int countL;
public:
Langas(float h, float w){
this->height=h;
this->widht=w;
countL++;
}
~Langas(){
--countL;
}
float getHeight(){
return height;
}
float getWidht(){
return widht;
}
static int getWindowCount(){
return countL;
}
};
class Durys{
private:
float heightD;
float widhtD;
static int countD;
public:
Durys(float hD, float wD){
this->heightD=hD;
this->widhtD=wD;
countD++;
}
~Durys(){
--countD;
}
float getHeightD(){
return heightD;
}
float getWidhtD(){
return widhtD;
}
static int getDoorCount(){
return countD;
}
};
class Kambarys{
private:
float heightK;
float widhtK;
float lenghtK;
public:
vector<Langas*> windows;
Durys* door;
Kambarys(float hK, float wK, float lK){
this->heightK=hK;
this->widhtK=wK;
this->lenghtK=lK;
}
Kambarys(const Kambarys &k){
this->door=k.door;
this->windows=k.windows;
heightK=k.heightK;
widhtK=k.widhtK;
lenghtK=k.lenghtK;
}
~Kambarys(){
door=NULL;
for(int i=0; i<windows.size(); i++){
delete windows[i];
}
windows.clear();
delete door;
}
float getHeightK(){
return heightK;
}
float getWidhtK(){
return widhtK;
}
float getLenghtK(){
return lenghtK;
}
void addWindow(Langas* w){
windows.push_back(w);
}
void addDoor(Durys *d){
door=d;
}
};
float countWallPlot(Kambarys* k){
float cWPlot=(2*k->getLenghtK()*k->getHeightK())+(2*k->getWidhtK()*k->getHeightK());
for(int i=0; i<k->windows.size(); i++){
cWPlot-=((k->windows[i]->getHeight()))*(k->windows[i]->getWidht());
}
cWPlot-=((k->door->getHeightD()))*(k->door->getWidhtD());
return cWPlot;
}
float countLenght(Kambarys* k){
float floorL=(k->getLenghtK()*k->getWidhtK()*2);
floorL-=(k->door->getWidhtD());
return floorL;
}
int Langas::countL=0;
int Durys::countD=0;
int main(){
Langas *langas1=new Langas(3.4, 1.2);
Durys *durys=new Durys(3.1, 1.5);
Langas *langas2=new Langas(6.4, 1.5);
Kambarys *k=new Kambarys(30.4, 40.1, 50.1);
Kambarys *k2=k;
k->addWindow(langas1);
k->addWindow(langas2);
k->addDoor(durys);
cout<<countWallPlot(k)<<" "<<countLenght(k)<<endl;
cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
k2=k;
delete k;
cout<<countWallPlot(k2)<<" "<<countLenght(k2)<<endl;
cout<<"Window count "<<Langas::getWindowCount()<<", door count "<<Durys::getDoorCount()<<endl;
}
You have to allocate memory for k2 and copy the object, not the pointer.
You have to allocate memory in the copy constructor and copy assignment operator.
door=NULL; before delete door; would skip the delete and cause a memory leak.
windows.clear(); is not necessary in the destructor. Keep your code simple.
EDIT: After you added "Perform the following steps: k2 = * k1; delete k1;" I made k2 an object, not a pointer.
#include <iostream>
#include <vector>
class Langas {
private:
float height;
float width;
static int count;
public:
Langas(float h, float w): height(h), width(w) {
++count;
}
~Langas() { --count; }
float getHeight() const { return height; }
float getWidht() const { return width; }
static int getWindowCount() { return count; }
};
class Durys {
private:
float height;
float width;
static int count;
public:
Durys(float h, float w): height(h), width(w) {
++count;
}
~Durys() { --count; }
float getHeight() const { return height; }
float getWidth() const { return width; }
static int getDoorCount() { return count; }
};
class Kambarys {
private:
float height;
float width;
float length;
public:
std::vector<Langas *> windows;
Durys *door = nullptr;
Kambarys(float hK, float wK, float lK): height(hK), width(wK), length(lK) {}
Kambarys(const Kambarys &k): height(k.height), width(k.width), length(k.length), windows(), door(k.door ? new Durys(k.door->getHeight(), k.door->getWidth()) : nullptr) {
for (const auto window : k.windows) {
windows.emplace_back(new Langas(window->getHeight(), window->getWidht()));
}
}
Kambarys &operator=(const Kambarys &k) {
door = k.door ? new Durys(k.door->getHeight(), k.door->getWidth()) : nullptr;
for (const auto window : k.windows) {
windows.emplace_back(new Langas(window->getHeight(), window->getWidht()));
}
height = k.height;
width = k.width;
length = k.length;
return *this;
}
~Kambarys() {
for (auto window : windows) {
delete window;
}
delete door;
}
float getHeight() const { return height; }
float getWidth() const { return width; }
float getLength() const { return length; }
void addWindow(Langas *w) { windows.emplace_back(w); }
void addDoor(Durys *d) { door = d; }
};
float countWallPlot(const Kambarys &k) {
float cWPlot = 2 * k.getLength() * k.getHeight() + 2 * k.getWidth() * k.getHeight();
for (const auto window : k.windows) {
cWPlot -= window->getHeight() * window->getWidht();
}
cWPlot -= k.door->getHeight() * k.door->getWidth();
return cWPlot;
}
float countLength(const Kambarys &k) {
float floor = k.getLength() * k.getWidth() * 2;
floor -= k.door->getWidth();
return floor;
}
int Langas::count = 0;
int Durys::count = 0;
int main() {
Langas *langas1 = new Langas(3.4, 1.2);
Durys *durys = new Durys(3.1, 1.5);
Langas *langas2 = new Langas(6.4, 1.5);
Kambarys *k = new Kambarys(30.4, 40.1, 50.1);
Kambarys k2(*k);
k->addWindow(langas1);
k->addWindow(langas2);
k->addDoor(durys);
std::cout << countWallPlot(*k) << " " << countLength(*k) << std::endl;
k2 = *k;
std::cout << "Window count " << Langas::getWindowCount() << ", door count " << Durys::getDoorCount() << std::endl;
delete k;
std::cout << countWallPlot(k2) << " " << countLength(k2) << std::endl;
std::cout << "Window count " << Langas::getWindowCount() << ", door count " << Durys::getDoorCount() << std::endl;
}

Why does my vector of pointers keep on resulting in a EXC_BAD_ACCESS?

I am attempting to create a graphical representation of finite automata using xcode, and as such I have created classes for states and transitions. In order to make moving objects easy, I have included a collection of pointers of transitions going in and out of the state. Compiling is fines, but when I try to append to the vector, it produces the following error. EXC_BAD_ACCESS(code=1, address=0x3f35)
Following the error takes me to the std library, and shows the error being in this line.
template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
void
vector<_Tp, _Allocator>::push_back(const_reference __x)
{
if (this->__end_ != this->__end_cap())
{
__annotate_increase(1);
__alloc_traits::construct(this->__alloc(),
_VSTD::__to_raw_pointer(this->__end_), __x);
++this->__end_;
}
else
__push_back_slow_path(__x);
}
Here is a simplified version of my State class, my Transition class is declared before and is then defined afterwards.
class State
{
int id;
std::vector<Transition *> links_in;
std::vector<Transition *> links_out;
float x;
float y;
int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
bool is_active = false;
bool is_end = false;
bool is_shown = true;
bool is_moving;
public:
// Get Functions go here
// Set Functions go here
//Add functions
void add_in_trans(Transition * t){
links_in.push_back(t);
}
void add_out_trans(Transition * t){
links_out.push_back(t);
}
//Delete Functions
void remove_in_trans(){
links_in.pop_back();
}
void remove_out_trans(){
links_out.pop_back();
}
void draw_state();
State(int ix, int iy);
State(){}
}
If you have any suggestions for a better way of doing this, I am more then happy to hear them. I have spent all day trying to sort this out, to no avail.
Thanks in advance.
UPDATE:
I attempted to use integers and vectors as a temporary fix, but I came up with the same problem, so I assume that the problem isn't the pointers but the way I'm using vectors.
This is the code
#include <vector>
class Transition;
class State
{
int id;
std::vector<int> links_in;
std::vector<int> links_out;
float x;
float y;
int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
bool is_active = false;
bool is_end = false;
bool is_shown = true;
bool is_moving;
public:
// Get Functions
int get_x(){
return x;
}
int get_y(){
return y;
}
int get_id(){
return id;
}
bool is_it_active(){
return is_active;
}
bool is_it_moving(){
return is_moving;
}
bool is_in(int ix, int iy){ //Function to tell if pair of coordinates are in the circle, used to select.
std::cerr << ix-x << " " << iy-y << " " << r*r << std::endl;
if ((ix-x)*(ix-x) + (iy-y)*(iy-y) < r*r)
return true;
else
return false;
}
// Set Functions
void set_active(bool s){
is_active = s;
}
void set_moving(bool s){
is_moving = s;
}
void end_switch(){
is_end = !is_end;
}
void set_start(){
g_start_state = id;
}
void set_x(int ix){
x = ix;
}
void set_y(int iy){
y = iy;
}
//Add functions
void add_in_trans(int t){
links_in.push_back(t);
}
void add_out_trans(int t){
links_out.push_back(t);
}
//Delete Functions
void remove_in_trans(){
links_in.pop_back();
}
void remove_out_trans(){
links_out.pop_back();
}
void draw_state();
State(int ix, int iy);
State(){}
};
State::State(int ix, int iy){
id = g_state_num;
if (g_start_state == 0)
g_start_state = id;
x = ix;
y = iy;
}
void State::draw_state(){
if (is_shown){
if (is_moving)
glTranslatef(g_cursor_x, g_cursor_y, 0.0);
else
glTranslatef(x, y, 0.0);
fill_colour();
if (is_active)
active_fill_colour();
glBegin(GL_POLYGON);
for (size_t i=0; i<24; i++){
float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
glVertex2fv(n);
}
glEnd();
line_colour();
glBegin(GL_LINES);
for (size_t i=0; i<24; i++){
float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
glVertex2fv(n);
}
glEnd();
if(is_end){
glPushMatrix();
glScalef(0.9, 0.9, 0.9);
for (size_t i=0; i<24; i++){
float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
glVertex2fv(n);
}
glPopMatrix();
}
text_colour();
std::string s = std::to_string(id);
for (int i=0; i<s.length(); i++){
glPushMatrix();
glTranslatef(-radius/2 + i*kerning, -radius/2, 0.0);
glScalef(0.3, 0.3, 1.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, s[i]);
glPopMatrix();
}
}
}
class Character{
int id;
char c;
public:
int get_id(){
return id;
}
char get_char(){
return c;
}
void set_char(char ic){
c = ic;
}
Character(char ic);
Character(){};
};
Character::Character(char ic){
id = g_character_num;
g_character_num++;
c = ic;
}
class Transition{
int ident;
State * from_state;
State * to_state;
float from[2];
float to[2];
Character c;
public:
void set_from(float x, float y){
from[0] = x;
from[1] = y;
}
void set_to(float x, float y){
to[0] = x;
to[1] = y;
}
void set_char(Character ic){
c = ic;
}
int get_id(){
return ident;
}
void draw_trans();
void set_trans(State * ifrom, State * ito, Character ic){
from_state = ifrom;
to_state = ito;
from[0] = ifrom->get_x();
from[1] = ifrom->get_y();
to[0] = ito->get_x();
to[1] = ito->get_y();
c = ic;
}
Transition(){};
Transition(State ifrom, State ito, Character ic){
from_state = &ifrom;
to_state = &ito;
from[0] = ifrom.get_x();
from[1] = ifrom.get_y();
to[0] = ito.get_x();
to[1] = ito.get_y();
c = ic;
}
};
void Transition::draw_trans(){
line_colour();
glBegin(GL_LINES);
glVertex2fv(from);
glVertex2fv(to);
glEnd();
float grad = (from[0] - to[0]) /(from[1] - to[1]); //(By finding the gradient of the slope, we can fin good place to show it's information, it's character.
if (grad < -1 || grad > 1){
glPushMatrix();
glTranslatef(from[0] - to[0] - 20, from[1] - to[1], 1.0);
}
else{
glPushMatrix();
glTranslatef(from[0] - to[0], from[1] - to[1] + 20, 1.0);
}
glutStrokeCharacter(GLUT_STROKE_ROMAN, (c.get_char()));
glPopMatrix();
}

CX0030: Error: Expression cannot be evaluated

I cannot modify the member values of a class object because of the above error:
When I call my initialize function to initialize my "garo" object, I receive the following run-time error,
"Unhandled exception at 0x01323976 in Heretic.exe: 0xC0000005: Access violation reading location 0x00000008."
NOTE: The class Garo is a child of Object.
THE CODE
Garo.h
#pragma once
#include "Object.h"
class Garo : public Object
{
private:
int animationRow;
public:
Garo();
void Destroy();
void Init(ALLEGRO_BITMAP *image = NULL);
void Update();
void Render();
void MoveLeft();
void MoveRight();
void Idle();
void SetAnimationRow(int row);
};
Garo.cpp
#include "Garo.h"
Garo::Garo()
{
Object::Init(20, 200, 3, 0, 0, 0, 16, 24);
}
void Garo::Init(ALLEGRO_BITMAP *image)
{
Object::Init(20, 200, 3, 0, 0, 0, 16, 24);
SetID(PLAYER);
SetAlive(true);
maxFrame = 3;
curFrame = 0;
frameWidth = 32;
frameHeight = 48;
animationColumns = 4;
animationDirection = 1;
animationRow = 0;
if(image != NULL)
Garo::image = image;
}
... the rest has been abbreviated
Object.h
#pragma once
#include <iostream>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>
#include "Globals.h"
class Object
{
private:
int ID;
bool alive;
bool collidable;
protected:
float x;
float y;
float velX;
float velY;
int dirX;
int dirY;
int boundX;
int boundY;
int maxFrame;
int curFrame;
int frameCount;
int frameDelay;
int frameWidth;
int frameHeight;
int animationColumns;
int animationDirection;
ALLEGRO_BITMAP *image;
public:
Object();
void virtual Destroy();
void Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY);
void virtual Update();
void virtual Render();
float GetX() {return x;}
float GetY() {return y;}
void SetX(float x) {Object::x = x;}
void SetY(float y) {Object::y = y;}
int GetBoundX() {return boundX;}
int GetBoundY() {return boundY;}
int GetID() {return ID;}
void SetID(int ID) {Object::ID = ID;}
bool GetAlive() {return alive;}
void SetAlive(bool alive) {Object::alive = alive;}
bool GetCollidable() {return collidable;}
void SetCollidable(bool collidable) {Object::collidable = collidable;}
bool CheckCollisions(Object *otherObject);
void virtual Collided(int objectID);
bool Collidable();
};
Object.cpp
#include "Object.h"
Object::Object()
{
x = 0;
y = 0;
velX = 0;
velY = 0;
dirX = 0;
dirY = 0;
boundX = 0;
boundY = 0;
maxFrame = 0;
curFrame = 0;
frameCount = 0;
frameDelay = 0;
frameWidth = 0;
frameHeight = 0;
animationColumns = 0;
animationDirection = 0;
image = NULL;
alive = true;
collidable = true;
}
void Object::Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY)
{
std::cout << "HERE?" << std::endl;
Object::x = x;
Object::y = y;
Object::velX = velX;
Object::velY = velY;
Object::dirX = dirX;
Object::dirY = dirY;
Object::boundX = boundX;
Object::boundY = boundY;
}
The Calling Code
Garo *garo;
// ...
garo->Init(garo_img);
This is where I receive the run-time error. I'm using Allegro libraries, so feel free to ask about any weird types you may see. I am stilling learning C++, so please help me to understand in rudimentary terms.
You need to instantiate an object instance to operate on. For example:
garo = new Garo();
By missing this out you are trying to invoke methods on an uninitialized variable. You should probably consider using some form of smart pointer to ensure that the object is destroyed.

Improper updation in C++, Allegro

LeftCollision in CheckCollision() goes true, when ever Object1 collides with Object2(square which is scrolling from right to left of screen) left side. But in the GameObject::Update()
Leftcollision never updates to True, though it changes to true in the CheckCollision section!!
What i want is, whenever the LeftCollision is true, Update should stop, until it becomes false again!!
Below is the code for Header file and CPP file!!
The problem is with LeftCollision updation!! Why is the value not reflected in the Update if condition!!
#pragma once
#include <iostream>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>
#include "Globals.h"
class GameObject
{
private :
int ID;
bool alive;
bool collidable;
protected :
float velX;
float velY;
int dirX;
int dirY;
int boundX;
int boundY;
int maxFrame;
int curFrame;
int frameCount;
int frameDelay;
int frameWidth;
int frameHeight;
int animationColumns;
int animationDirection;
ALLEGRO_BITMAP *image;
public :
float x;
float y;
bool LeftCollision;
bool pause;
GameObject();
void virtual Destroy();
void Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY);
void virtual Update();
void virtual Render();
float GetX() {return x;}
float GetY() {return y;}
void SetX(float x) {GameObject ::x = x;}
void SetY(float y) {GameObject ::y = y;}
int GetBoundX() {return boundX;}
int GetBoundY() {return boundY;}
int GetID() {return ID;}
void SetID(int ID) {GameObject::ID = ID;}
bool GetAlive() {return alive;}
void SetAlive(bool alive) {GameObject :: alive = alive;}
bool GetCollidable() {return collidable;}
void SetCollidable(bool collidable) {GameObject :: collidable = collidable;}
bool CheckCollisions(GameObject *otherObject);
void virtual Collided(int objectID);
bool Collidable();
};
#include "GameObject.h"
GameObject :: GameObject()
{
x = 0;
y = 0;
velX = 0;
velY = 0;
dirX = 0;
dirY = 0;
boundX = 0;
boundY = 0;
LeftCollision = false;
maxFrame = 0;
curFrame = 0;
frameCount = 0;
frameDelay = 0;
frameWidth = 0;
frameHeight = 0;
animationColumns = 0;
animationDirection = 0;
image = NULL;
alive = true;
collidable = true;
}
void GameObject :: Destroy()
{
}
void GameObject :: Init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY)
{
GameObject :: x = x;
GameObject :: y = y;
GameObject :: velX = velX;
GameObject :: velY = velY;
GameObject :: dirX = dirX;
GameObject :: dirY = dirY;
GameObject :: boundX = boundX;
GameObject :: boundY = boundY;
}
void GameObject :: Update()
{
if(LeftCollision == false)
{
x += velX * dirX;
y += velY * dirY;
}
}
void GameObject :: Render()
{
}
bool GameObject :: CheckCollisions(GameObject *otherObject)
{
float oX = otherObject->GetX();
float oY = otherObject->GetY();
int obX = otherObject->GetBoundX();
int obY = otherObject->GetBoundY();
if( x + boundX > oX - obX &&
x - boundX < oX + obX &&
y + boundY > oY - obY &&
y - boundY < oY + obY && otherObject->GetID() == TRIANGLE)
{
return true;
}
else if(((oX < x + boundX + 14)&&(oX+ 40 >x - boundX))&&!((oY < y + boundY)&&(oY+40 > y - boundY))
&& otherObject->GetID() == SQUARE)
{
y = oY - boundX - 10;
//x = oX + 40;
return true;
}
else if((oX < x + boundX + 14) && (oX > x - boundX) && otherObject->GetID() == SQUARE)
{
LeftCollision = true;
return false;
}
else
{
return false;
LeftCollision = false;
}
}
void GameObject :: Collided(int objectID)
{
}
bool GameObject :: Collidable()
{
return alive && collidable;
}
Is this part of your problem - returning without setting the value
else
{
return false;
LeftCollision = false;
}
The only thing that jumps to mind is that you've got more than one object going around and you're not always using the one you expect. Try putting something like:
printf("here(%s:%d) this=%p\n", __FILE__, __LINE__, this);
at the start of each method to make sure that the this point is what you think it is. The problem may lie in the calling code that you haven't shown.