unknown type error in C++

Pier picture Pier · Feb 7, 2012 · Viewed 9.1k times · Source

What is going on?

#include "MyClass.h"

class MyOtherClass {
  public:
    MyOtherClass();
    ~MyOtherClass();

    MyClass myVar; //Unknown type Error
};

Suddenly when I include the .h and write that var Xcode gives me tons of errors... and also the unknown type error.

How can it be unknown when the .h is included right there?

Here is the NodeButton.h file which would correspond to the MyClass.h in the example

#pragma once

#include "cinder/Vector.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Color.h"
#include "cinder/ImageIo.h"
#include "cinder/Timeline.h"
#include "cinder/app/AppBasic.h"
#include "cinder/App/App.h"

#include "Node.h"
#include "CursorMano.h"

using namespace ci;
using namespace ci::app;
using namespace std;
using namespace is;

typedef boost::shared_ptr<class NodeButton> NodeButtonRef;


class NodeButton  : public Node2D 
{
    public:
        NodeButton (CursorMano *cursor, string imageUrl, bool fadeIn = false, float delay = 0.0f);
        virtual ~NodeButton ();

        //methods
        void update( double elapsed );
        void draw();
        void setup();

        //events
        bool mouseMove( ci::app::MouseEvent event );

        //vars
        CursorMano      *mCursor;
        gl::Texture     mImageTexture;
        Anim<float>     mAlpha = 1.0f;
        bool            mSelected = false;

    private:
};

And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.

#pragma once

#include <list>
#include <vector>

#include "cinder/app/AppBasic.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"

#include "NodeButton.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class CursorMano {
    public:
        CursorMano (AppBasic *app);
        ~CursorMano ();

        void    mueveMano(Vec2i);
        void    update();
        void    draw();
        void    play(int button);
        void    reset(int button);

        Vec2i   mMousePos;
        NodeButton                  mButtonCaller; //this gives the unknow type error

    private:
        AppBasic                    *mApp;
        gl::Texture                 mFrameTexture;
        qtime::MovieGl              mMovie;
        int                         mIdButton;
};

Answer

Alok Save picture Alok Save · Feb 7, 2012

You have a circular dependency of your header files.

NodeButton.h defines NodeButton class which CursorMano.h needs to include so that compiler can see definition for NodeButton but NodeButton.h itself includes CursorMano.h.

You will need to use forward declarations to break this circular dependency.

In NodeButton.h you just use an pointer to CursorMano so You do not need to include the CursorMano.h just forward declare the class after the using namespace declarations.

using namespace std;
using namespace is;

class CursorMano;