Compiler says class has no constructor while it actually has one

Started by
19 comments, last by lucky6969b 10 years, 11 months ago


activity = new Idle(this, NULL);


class Idle : public Activity

{

private:

    float mTimeInIdle;



public:

    Idle() : mTimeInIdle(0)    { }

    Idle(Objects *actor, Goods *target) : Activity(actor, target)

    {



    }

Error 1 error C2514: 'Idle' : class has no constructors d:\jacky\documents\visual studio 2010\projects\perfectsim\perfectsim\perfectsim\Objects\Objects.h 43 1 PerfectSim

The activity = new Idle(this, NULL) line is located inside the Objects::Objects(...) constructor.

Would it be caused by some cyclic dependencies? How do I go about resolving it?

Thanks

Jack

Advertisement

Sounds like you only forward declared the class and didn't include the full class definition (include the header that defines the class in the source files that call new Idle()).

If that gives you a circular dependency you will have to look at your class dependencies (you could make a class factory that is responsible for returning new Idle objects, for example).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I put everything inside header files. like Objects.h and Idle.h


#pragma once

#include "..\SkinnedMesh\skinnedMesh.h"
#include "..\Activities\Activity.h"
#include "DetourNavMeshQuery.h"
#include "MotiveBase.h"

//#include "..\myDX9Widget.h"

class myDX9Widget;
class Idle;

class Objects
{
public:
    Objects() : m_pAnimCtrl(0), energy(0) { }

    Objects(SkinnedMesh* _mesh) : m_pAnimCtrl(0)
    {
        SetPos(D3DXVECTOR3(0,0,0));
        SetRot(D3DXVECTOR3(0,0,0));
        SetRotCenter(D3DXVECTOR3(0,0,0));
        SetScale(D3DXVECTOR3(1,1,1));
        SetScaleCenter(D3DXVECTOR3(1,1,1));

        D3DXQUATERNION quat;
        D3DXQuaternionIdentity(&quat);

        SetRotQuat(quat);
        
        
        if (_mesh->GetMasterAnimCtrl() != NULL)
        {
            _mesh->GetMasterAnimCtrl()->CloneAnimationController(
            _mesh->GetMasterAnimCtrl()->GetMaxNumAnimationOutputs(),
            _mesh->GetMasterAnimCtrl()->GetMaxNumAnimationSets(),
            _mesh->GetMasterAnimCtrl()->GetMaxNumTracks(),
            _mesh->GetMasterAnimCtrl()->GetMaxNumEvents(),
            &m_pAnimCtrl);
        }
        

        activity = new Idle(this, NULL);
        
        mesh = _mesh;
    }

Then you need to include Idle.h in objects.h. You can't just forward declare it. You should use a cpp file for the implementations of the functions though and just declare them in the class definition, you are going to end up with problems with dependencies soon if you carry on with the "all in a header" approach.

If Idle needs to use member functions/variables in Objects and vice versa you are going to end up with a circular dependency which will require separation of the function body from the in-class declaration.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Now that I've included Idle.h in Objects.h

so that Objects should see everything in idle, that's point 1

Point 2, idle can't see everything in Objects. So I forward declare it, otherwise, I have a circular references, Objects->Idle and Idle->Objects

How do I make Idle see Objects so I won't get the following errors:

Error 1 error C2027: use of undefined type 'Objects' d:\jacky\documents\visual studio 2010\projects\perfectsim\perfectsim\perfectsim\Activities\Idle.h 29 1 PerfectSim
Error 3 error C2027: use of undefined type 'Objects' d:\jacky\documents\visual studio 2010\projects\perfectsim\perfectsim\perfectsim\Activities\Idle.h 33 1 PerfectSim
Error 10 error C2027: use of undefined type 'Objects' d:\jacky\documents\visual studio 2010\projects\perfectsim\perfectsim\perfectsim\activities\Idle.h 29 1 PerfectSim

You can break the circular dependency if you split your implementation and declaration into .cpp and .h-files

Only declare the classes in the header-files, no method implementations.

You can have only a forward declaration in the .h-file if you only have pointers to the classes in the .h-file

Then include the full header in the cpp-file to be able to call its methods.

I put everything inside header files.

That simply will not work in C++ in general. Any code in Objects.h that needs to see more than the forward declaration should be moved into an implementation file which can see the whole interface of Idle.

There are languages which do not require (or even allow) the separation between header and implementation that C++ usually requires, but I would suggest switching languages if you really want it that badly. Trying to force it on C++ will just lead to pain and suffering.

Please do a quick check on my coding

Activity.h

#include <string>
 

class Idle;
class Objects;
class Goods;

class Activity
{
public:
    Objects *Actor;
    Goods *Target;
    Activity() : Actor(0), Target(0) { }
    Activity( Objects* actor, Goods* target )
    {
        Actor = actor;
        Target = target;
    }

    virtual bool OnUpdate(float seconds) = 0;

    void Update(float seconds);
    

    Activity *FindBestActivity(Objects *actor);
    
    std::string ToString();

};

Idle.h

#include "Activity.h"
 
 
 

#define MAX_IDLE_TIME 8
#define ACTIVITY_IDLE_ENERGY 1.0

class Objects;
class Activity;

class Idle : public Activity
{
private:
    float mTimeInIdle;

public:
    Idle() : mTimeInIdle(0)    { }
    Idle(Objects *actor, Goods *target);

    bool OnUpdate(float seconds);
    

};

idle.cpp

#include "Idle.h"

 

Idle::Idle(Objects* actor, Goods *target) : Activity(actor, target)
{

}

bool Idle::OnUpdate(float seconds)
{

    mTimeInIdle += seconds;
    if (mTimeInIdle >= MAX_IDLE_TIME)
    {
        Actor->activity = FindBestActivity(Actor);
    }


    Actor->energy += ACTIVITY_IDLE_ENERGY * seconds;
    return false;
}

Error 1 error C2027: use of undefined type 'Objects' D:\Jacky\Documents\Visual Studio 2010\Projects\PerfectSim\PerfectSim\PerfectSim\Activities\Idle.cpp 16 1 PerfectSim
Error 3 error C2027: use of undefined type 'Objects' D:\Jacky\Documents\Visual Studio 2010\Projects\PerfectSim\PerfectSim\PerfectSim\Activities\Idle.cpp 20 1 PerfectSim
Error 2 error C2227: left of '->activity' must point to class/struct/union/generic type D:\Jacky\Documents\Visual Studio 2010\Projects\PerfectSim\PerfectSim\PerfectSim\Activities\Idle.cpp 16 1 PerfectSim
Error 4 error C2227: left of '->energy' must point to class/struct/union/generic type D:\Jacky\Documents\Visual Studio 2010\Projects\PerfectSim\PerfectSim\PerfectSim\Activities\Idle.cpp 20 1 PerfectSim

I've moved everything into implementation file. Here now are what I've got.

Thanks

From quickly glancing over it the problem seems to be the obvious failure to include Activity.h in Idle.cpp.

From quickly glancing over it the problem seems to be the obvious failure to include Activity.h in Idle.cpp.

Idle.cpp includes Idle.h, which in turn includes Activity.h.

It seems that the problem is that you're dereferencing the Objects class in the Idle::OnUpdate() method, while you only have a forward declaration. You'll need to include the header for all objects that you will need more than a pointer or reference to in order to use them. If this causes a circular dependency or circular header-inclusion, then the hierarchy of files needs to be fixed.

This topic is closed to new replies.

Advertisement