Getting error C2512 : no appropriate default constructor

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

Hi there Ogre Forums. I'm new here, but I've been programming in Ogre for about a year now. This is the first time I've had this error. I'm using ogre to make a game. My game has several types of player controlled and autonomously controlled objects. My player class objects are not giving errors, but my auto objects are giving error C2512.

AutoObject

Code: Select all

class AutoObject : public IMovingObject
{
public:

	AutoObject();
	~AutoObject();

	//Inherited from IGameManager
	void Initialize();
	void UpdateStarted(const Ogre::FrameEvent& gameTime);
	void UpdateEnded(const Ogre::FrameEvent& gameTime);
	void Cleanup();

	//Inherited from IObject
	void Draw();
	bool HandleMessage(int message);
	Ogre::Vector3 GetPosition();
	void SetPostion(Ogre::Vector3 pos);
	double GetBRadius();
	void SetBRadius(double r);
	int GetID();
	void SetID(int id);
	bool IsTagged();
	void Tag();
	void UnTag();
	Ogre::Vector3 GetScale();
	void SetScale(Ogre::Vector3 nScale);
	void SetScale(double nScale);
	ObjectType GetObjectType();
	void SetObjectType(ObjectType nType);
	void Write(std::ostream& os);
	void Read(std::istream& is);
	
	//Inherited from IMovingObject
	Ogre::Vector3 GetVelocity();
	void SetVelocity(Ogre::Vector3 nVelocity);
	double GetMass();
	void SetMass(double nMass);
	Ogre::Vector3 GetPerp();
	double GetMaxSpeed();
	void SetMaxSpeed(double nSpeed);
	double GetMaxForce();
	void SetMaxForce(double nForce);
	void SetAppliedForce(Ogre::Vector3 nForce);
	Ogre::Vector3 GetAppliedForce();
	bool IsSpeedMaxedOut();
	double Speed();
	double SpeedSq();
	Ogre::Vector3 GetHeading();
	void SetHeading(Ogre::Vector3 nHeading);
	void SetHeading(int xHeading, int yHeading, int zHeading);
	bool RotateHeadingToFacePosition(Ogre::Vector3 target);
	double GetMaxTurnRate();
	void SetMaxTurnRate(double nRate);
	Ogre::Vector3 GetCurrentAccel();
	void SetCurrentAccel(Ogre::Vector3 nAccel);
	void SetCurrentAccel(double xAccel, double yAccel, double zAccel);

	//Unique to AutoObject
	SteeringBehaviors* Steering();
	Ogre::Vector3 SmoothedHeading();
	bool isSmoothingOn();
	void SmoothingOn();
	void SmoothingOff();
	void ToggleSmoothing();
	double TimeElapsed();

private:
	int m_ID;					//Unique, assigned by ObjectManager

	ObjectType m_ObjectType;	//A Auto object is designated by 
								//Autonomous, of the ObjectType enum
								//in IObject
	bool m_Tag;					

	Ogre::Vector3 m_Pos;		//Indicates current position described by
								//a 3D vector

	Ogre::Vector3 m_Scale;		//Indicates the current scale described by
								//a 3D vector
	
	double m_BoundingRadius;

	Ogre::Vector3 m_Velocity;	//The current velocity described by a 3D 
								//vector

	Ogre::Vector3 m_Heading;	//The current heading described by a 3D 
								//vector

	Ogre::Vector3 m_Perp;		//A unit vector perpendicular 
								//to the heading

	Ogre::Vector3 m_Acceleration;

	Ogre::Vector3 m_AppliedForce;

	double m_Mass;				

	double m_MaxSpeed;			

	double m_MaxForce;

	double m_MaxTurnRate;

	SteeringBehaviors* m_Steering;

	Ogre::Vector3 m_HeadingSmoother;

	Ogre::Vector3 m_SmoothedHeading;

	bool m_SmoothingOn;

	double m_TimeElapsed;

};

Code: Select all

AutoObject::AutoObject() :
	m_ObjectType(AUTONOMOUS),
	m_Tag(false),
	m_Pos(Ogre::Vector3::ZERO),
	m_Scale(Ogre::Vector3(1)),
	m_BoundingRadius(1.0),
	m_Velocity(Ogre::Vector3::ZERO),
	m_Heading(Ogre::Vector3::ZERO),
	m_Perp(Ogre::Vector3::ZERO),
	m_Acceleration(Ogre::Vector3::ZERO),
	m_AppliedForce(Ogre::Vector3::ZERO),
	m_Mass(1.0),
	m_MaxSpeed(3000.0),
	m_MaxForce(1000.0),
	m_MaxTurnRate(1000.0),
	m_HeadingSmoother(Ogre::Vector3::ZERO),
	m_SmoothedHeading(Ogre::Vector3::ZERO),
	m_SmoothingOn(false),
	m_TimeElapsed(0.0)
{
	m_ID = GameServices::ObjectManager()->GetAutonomousObjectCount();
}
The constructor is declared basically the same way for my Player objects, the only difference is a few extra initializations for members unique to my AutoObject class. Both the Player and Auto classes inherit IGameManager, IObject and IMovingObject.

Any suggestions?
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by CABAListic »

This means that some object in your AutoObject does not have a default constructor. First candidate would be the base class - is there a default constructor for IMovingObject? If not, you need to explicitly call one of the defined ones.

If it's not the base class, then it can be any of the class members, particularly those you do not explicitly initialise in the constructor.
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Re: Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

IObject and IMovingObject are abstract classes, so I don't have constructors in those. Also I have a PlayerObject class that inherits IObject and IMovingObject exactly like AutoObject does and my PlayerObjects are being created without any problems.

I get the error whenever I call

AutoObject* Obj = new AutoObject();

I do have the default constructor for AutoObject defined.... So I'm still stumped. I'm also pretty sure I initialize all the members of AutoObject in the constructor.

Code: Select all

class IObject: public IGameManager
{
public:
	virtual void Draw()=0;
	virtual bool HandleMessage(int message)=0;
	virtual Ogre::Vector3 GetPosition()=0;
	virtual void SetPostion(Ogre::Vector3 pos)=0;
	virtual double GetBRadius()=0;
	virtual void SetBRadius(double r)=0;
	virtual int GetID()=0;
	virtual void SetID(int id)=0;
	virtual bool IsTagged()=0;
	virtual void Tag()=0;
	virtual void UnTag()=0;
	virtual Ogre::Vector3 GetScale()=0;
	virtual void SetScale(Ogre::Vector3 nScale)=0;
	virtual void SetScale(double nScale)=0;
	virtual ObjectType GetObjectType()=0;
	virtual void SetObjectType(ObjectType nType)=0;
	virtual void Write(std::ostream& os)=0;
	virtual void Read(std::istream& is)=0;
};

Code: Select all

class IMovingObject: public IObject
{
public:
	virtual Ogre::Vector3 GetVelocity()=0;
	virtual void SetVelocity(Ogre::Vector3 nVelocity)=0;
	virtual double GetMass()=0;
	virtual void SetMass(double nMass)=0;
	virtual Ogre::Vector3 GetPerp()=0;
	virtual double GetMaxSpeed()=0;
	virtual void SetMaxSpeed(double nSpeed)=0;
	virtual double GetMaxForce()=0;
	virtual void SetMaxForce(double nForce)=0;
	virtual void SetAppliedForce(Ogre::Vector3 nForce)=0;
	virtual Ogre::Vector3 GetAppliedForce()=0;
	virtual bool IsSpeedMaxedOut()=0;
	virtual double Speed()=0;
	virtual double SpeedSq()=0;
	virtual Ogre::Vector3 GetHeading()=0;
	virtual void SetHeading(Ogre::Vector3 nHeading)=0;
	virtual void SetHeading(int xHeading, int yHeading, int zHeading)=0;
	virtual bool RotateHeadingToFacePosition(Ogre::Vector3 target)=0;
	virtual double GetMaxTurnRate()=0;
	virtual void SetMaxTurnRate(double nRate)=0;
	virtual Ogre::Vector3 GetCurrentAccel()=0;
	virtual void SetCurrentAccel(Ogre::Vector3 nAccel)=0;
	virtual void SetCurrentAccel(double xAccel, double yAccel, double zAccel)=0;
};
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by CABAListic »

The compiler error should state explicitly which object is at fault. Does it say "error C2512: 'AutoObject'", or does it mention a different type?
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Re: Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

Right, It does say it is the AutoObject class with the error

error C2512: 'AutoObject' : no appropriate default constructor available
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by CABAListic »

Then apparently it doesn't see a default constructor for AutoObject. If your quoted declarations are accurate, that is weird. The only thing I could think of is a name clash with some other class named AutoObject coming from somewhere else. You might try to rename the class, if only to test this.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by Klaim »

Could it be because you used forward declarations and didn't include the header? It shouldn't generate this error, on VS2012 I would have an error saying I'm using an "incomplete type" but I don't know which version of VS you are using?
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
x 58
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by CABAListic »

No, in that case the error he'd be getting would be different, just as you said. Even in older VS versions :)
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Re: Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

I am using a forward deceleration of AutoObject in my SteeringBehaviors class.

Code: Select all

class PlayerObject;
class AutoObject;
class StationaryObject;

class SteeringBehaviors
{
public:
	SteeringBehaviors(AutoObject* agent);
	~SteeringBehaviors();

        .
        .
        .
}
My AutoObject class includes SteeringBehaviors.h and my PlayerObject class doesn't, so that would explain why AutoObjects have problems and PlayerObjects don't, But how should I go about fixing this. They are all separated into their own .h files so I really have not control over which compiles first...
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Re: Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

Got it figured out. I included AutoObject.h in my SteeringBehaviors class instead of the forward declaration and friended the SteeringBehaviors class in AutoObject instead of including the SteeringBehaviors.h

Code: Select all

class AutoObject : public IMovingObject
{
	friend class SteeringBehaviors;
        .
        .
        .
}
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by Klaim »

This is not a good solution at all and don't explain what was the problem exactly.

Can you show us the full headers that didn't work?
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Re: Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

Well they are all pretty big... but here is what wasn't playing nice together


AutoObject.h

Code: Select all

#pragma once

#include "GameStd.h"
#include "SteeringBehaviors.h"
#include <OgreVector3.h>



class AutoObject : public IMovingObject
{
	
public:

	AutoObject();
	~AutoObject();

	//Inherited from IGameManager
	void Initialize();
	void UpdateStarted(const Ogre::FrameEvent& gameTime);
	void UpdateEnded(const Ogre::FrameEvent& gameTime);
	void Cleanup();

	//Inherited from IObject
	void Draw();
	bool HandleMessage(int message);
	Ogre::Vector3 GetPosition();
	void SetPostion(Ogre::Vector3 pos);
	double GetBRadius();
	void SetBRadius(double r);
	int GetID();
	void SetID(int id);
	bool IsTagged();
	void Tag();
	void UnTag();
	Ogre::Vector3 GetScale();
	void SetScale(Ogre::Vector3 nScale);
	void SetScale(double nScale);
	ObjectType GetObjectType();
	void SetObjectType(ObjectType nType);
	void Write(std::ostream& os);
	void Read(std::istream& is);
	
	//Inherited from IMovingObject
	Ogre::Vector3 GetVelocity();
	void SetVelocity(Ogre::Vector3 nVelocity);
	double GetMass();
	void SetMass(double nMass);
	Ogre::Vector3 GetPerp();
	double GetMaxSpeed();
	void SetMaxSpeed(double nSpeed);
	double GetMaxForce();
	void SetMaxForce(double nForce);
	void SetAppliedForce(Ogre::Vector3 nForce);
	Ogre::Vector3 GetAppliedForce();
	bool IsSpeedMaxedOut();
	double Speed();
	double SpeedSq();
	Ogre::Vector3 GetHeading();
	void SetHeading(Ogre::Vector3 nHeading);
	void SetHeading(int xHeading, int yHeading, int zHeading);
	bool RotateHeadingToFacePosition(Ogre::Vector3 target);
	double GetMaxTurnRate();
	void SetMaxTurnRate(double nRate);
	Ogre::Vector3 GetCurrentAccel();
	void SetCurrentAccel(Ogre::Vector3 nAccel);
	void SetCurrentAccel(double xAccel, double yAccel, double zAccel);

	//Unique to AutoObject
	SteeringBehaviors* Steering();
	Ogre::Vector3 SmoothedHeading();
	bool isSmoothingOn();
	void SmoothingOn();
	void SmoothingOff();
	void ToggleSmoothing();
	double TimeElapsed();

private:
	int m_ID;					//Unique, assigned by ObjectManager

	ObjectType m_ObjectType;	//A Auto object is designated by 
								//Autonomous, of the ObjectType enum
								//in IObject
	bool m_Tag;					

	Ogre::Vector3 m_Pos;		//Indicates current position described by
								//a 3D vector

	Ogre::Vector3 m_Scale;		//Indicates the current scale described by
								//a 3D vector
	
	double m_BoundingRadius;

	Ogre::Vector3 m_Velocity;	//The current velocity described by a 3D 
								//vector

	Ogre::Vector3 m_Heading;	//The current heading described by a 3D 
								//vector

	Ogre::Vector3 m_Perp;		//A unit vector perpendicular 
								//to the heading

	Ogre::Vector3 m_Acceleration;

	Ogre::Vector3 m_AppliedForce;

	double m_Mass;				

	double m_MaxSpeed;			

	double m_MaxForce;

	double m_MaxTurnRate;

	SteeringBehaviors* m_Steering;

	Ogre::Vector3 m_HeadingSmoother;

	Ogre::Vector3 m_SmoothedHeading;

	bool m_SmoothingOn;

	double m_TimeElapsed;

};
SteeringBehaviors.h

Code: Select all

#pragma once

#include "GameStd.h"
#include "Path.h"
#include <OgreVector3.h>

class PlayerObject;
class AutoObject;
class StationaryObject;

const double WANDER_RADIUS = 1.2;
const double WANDER_DISTANCE = 2.0;
const double WANDER_JITTER = 80.0;
const double WAYPOINT_SEEK_DISTANCE = 20;

class SteeringBehaviors
{
public:
	enum SummingMethod{WEIGHTED_AVERAGE, PRIORITIZED, DITHERED};

	SteeringBehaviors(AutoObject* agent);
	~SteeringBehaviors();
	Ogre::Vector3 Calculate();
	double ForwardComponent();
	double SideComponent();
	void SetTarget(Ogre::Vector3 Target);
	void SetTargetAgent1(IMovingObject* agent);
	void SetTargetAgent2(IMovingObject* agent);
	void SetOffset(Ogre::Vector3 offset);
	Ogre::Vector3 GetOffset();
	void SetPath(std::list<Ogre::Vector3> newPath);
	void CreateRandomPath(int numOfWaypoints, int mx, int my, int cx, int cy);
	Ogre::Vector3 Force();
	void ToggleSpacePartitioning();
	bool isSpacePartitioningOn();
	void SetSummingMethod(SummingMethod sm);

	void SeekOn();
	void PursuitOn(IMovingObject* agent);
	void ArriveOn();
	void InterposeOn(IMovingObject* agent1, IMovingObject* agent2);
	void OffsetPursuitOn(IMovingObject* agent, Ogre::Vector3 newoffset);
	void FleeOn();
	void EvadeOn(IMovingObject* agent1);
	void HideOn(IMovingObject* agent);
	void WallAvoidanceOn();
	void PathFollowingOn();
	void WanderOn();
	void SeperationOn();
	void AlignmentOn();
	void CohesionOn();
	void FlockingOn();

	void SeekOff();
	void PursuitOff();
	void ArriveOff();
	void InterposeOff();
	void OffsetPursuitOff();
	void FleeOff();
	void EvadeOff();
	void HideOff();
	void WallAvoidanceOff();
	void PathFollowingOff();
	void WanderOff();
	void SeperationOff();
	void AlignmentOff();
	void CohesionOff();
	void FlockingOff();

	bool isSeekOn();
	bool isPursuitOn();
	bool isArriveOn();
	bool isInterposeOn();
	bool isOffsetPursuitOn();
	bool isFleeOn();
	bool isEvadeOn();
	bool isHideOn();
	bool isWallAvoidanceOn();
	bool isPathFollowingOn();
	bool isWanderOn();
	bool isSeperationOn();
	bool isAlignmentOn();
	bool isCohesionOn();
	bool isFlockingOn();

	double GetDBoxLength();
	std::vector<Ogre::Vector3> GetFeelers();
	double GetWanderJitter();
	double GetWanderDistance();
	double GetWanderRadius();

	double GetSeperationWeight();
	double GetAlignmentWeight();
	double GetCohesionWeight();

private:
	enum BehaviorType{
		none			= 0x00000,
		seek			= 0x00002,
		pursuit			= 0x00004,
		arrive			= 0x00008,
		interpose		= 0x00010,
		offset_pursuit	= 0x00020,
		flee			= 0x00040,
		evade			= 0x00080,
		hide			= 0x00100,
		wall_avoid		= 0x00200,
		path_following	= 0x00400,
		wander			= 0x00800,
		seperation		= 0x01000,
		alignment		= 0x02000,
		cohesion		= 0x04000,
		flocking		= 0x08000
	};

	//A pointer to the owner of this instance
	AutoObject* m_Vehicle;

	Ogre::Vector3 m_SteeringForce;

	IMovingObject* m_TargetAgent1;

	IMovingObject* m_TargetAgent2;

	Ogre::Vector3 m_Target;

	std::vector<Ogre::Vector3> m_Feelers;

	double m_WallDetectionFeelerLength;

	Ogre::Vector3 m_WanderTarget;

	double m_WanderJitter;
	
	double m_WanderRadius;

	double m_WanderDistance;

	double m_WeightSeek;

	double m_WeightPursuit;

	double m_WeightArrive;

	double m_WeightInterpose;

	double m_WeightOffsetPursuit;

	double m_WeightFlee;

	double m_WeightEvade;

	double m_WeightHide;

	double m_WeightWallAvoid;

	double m_WeightPathFollowing;

	double m_WeightWander;

	double m_WeightSeperation;

	double m_WeightAlignment;

	double m_WeightCohesion;

	double m_WeightFlocking;

	double m_ViewDistance;

	Path* m_Path;

	double m_WaypointSeekDistSq;

	Ogre::Vector3 m_Offset;

	int m_Flags;

	enum Deceleration{SLOW,NORMAL,FAST};

	Deceleration m_Deceleration;

	bool m_CellSpaceOn;

	SummingMethod m_SummingMethod;

	double m_DBoxLength;

	double PanicDistanceSq;

	double ThreatRange;

	bool On(BehaviorType bt);

	bool AccumulateForce(Ogre::Vector3 &force, Ogre::Vector3 forceToAdd);

	void CreateFeelers();

	/* BEGIN BEHAVIOR DECLARATIONS */
	Ogre::Vector3 Seek(Ogre::Vector3 TargetPos);

	Ogre::Vector3 Pursuit(IMovingObject* agent);

	Ogre::Vector3 Arrive(Ogre::Vector3 TargetPos, Deceleration decel);

	Ogre::Vector3 Interpose(IMovingObject* agentA, IMovingObject* agentB);

	Ogre::Vector3 OffsetPursuit(IMovingObject* agent, Ogre::Vector3 offset);

	Ogre::Vector3 Flee(Ogre::Vector3 hunterPos)  ;

	Ogre::Vector3 Evade(IMovingObject* agent);

	Ogre::Vector3 Hide(IMovingObject* hunter, std::vector<IObject*> &obstacles);

	Ogre::Vector3 WallAvoidance(std::vector<StationaryObject> &walls);

	Ogre::Vector3 FollowPath();

	Ogre::Vector3 Wander();

	Ogre::Vector3 Seperation(std::vector<IMovingObject*> &agents);

	Ogre::Vector3 Alignment(std::vector<IMovingObject*> &agents);

	Ogre::Vector3 Cohesion(std::vector<IMovingObject*> &agents);

	Ogre::Vector3 Flocking(std::vector<IMovingObject*> &agents);

	/* END BEHAVIOR DECLARATIONS */

	Ogre::Vector3 CalculateWeightedSum();

	Ogre::Vector3 CalculatePrioritized();

	Ogre::Vector3 CalculateDithered();

	Ogre::Vector3 GetHidingPosition(Ogre::Vector3 posOb, double radiusOb, Ogre::Vector3 posHunter);

};
My PlayerObject class and StationaryObject class don't have any issues, but my AutoObject produces the error. My guess is because Player and Stationary don't include the SteeringBehaviors class since my player moves by key presses and my stationary objects don't move.

The friend solution seems to have worked. Any other suggestions?

(This code is a modified version of the steering behaviors project found in the book Programming Game AI by Example by Mat Buckland)
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by Klaim »

I just quickly read but what I see is that you could remove the include of steeringbehaviour.h from autoobject.h and add a forward declare of steeringbehaviour class instead.
Then just make sure that the compilation unit (the cpp file) does include the right header: autoobject.cpp and steeringbehaviour.cpp should both include autoobject.h and steeringbehaviour.h

I don't see why you need a friend class here, there should be no need for such overkill feature.
User avatar
DigitalRock
Gnoblar
Posts: 7
Joined: Mon Mar 12, 2012 12:03 am

Re: Getting error C2512 : no appropriate default constructor

Post by DigitalRock »

Thanks for the advice. I tried it and that worked just fine.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: Getting error C2512 : no appropriate default constructor

Post by Klaim »

So it was a problem of headers coupled with forward declarations? Has CAbalistic and me said before it should have been another error.

Oh well, I guess it's VS not being the best error reporter now. :)
Post Reply