Reputation: 5
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!!!
Upvotes: 0
Views: 196
Reputation: 303027
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 *
"
Upvotes: 1