C++中的未知类型错误

3

发生了什么?

#include "MyClass.h"

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

    MyClass myVar; //Unknown type Error
};

当我包含.h并写入变量时,Xcode会给出大量错误提示...还有未知类型的错误。

既然.h已经包含在里面,为什么还会是未知的呢?

这里是NodeButton.h文件,对应于示例中的MyClass.h。

#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:
};

这里是CursorMano.h的内容,对应于示例中的MyOtherClass.h。

#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;
};

4
MyClass.h 的内容是什么?同时,MyOtherClass 的拼写和其构造函数-析构函数不匹配! - iammilind
我们需要更多的代码来帮助解决这个问题。你可以展示一下MyClass.h文件的全部内容及这个头文件的全部内容吗? - templatetypedef
这将主要是因为构造函数和析构函数的拼写更改。 - Rohit Vipin Mathews
那么,这是代码...有人能帮忙吗? - Pier
@Als 我已经编辑了我的问题,加入了真实的代码。你也想要发生错误的.h文件吗? - Pier
显示剩余3条评论
2个回答

7
您的头文件存在循环依赖性。 NodeButton.h 定义了 NodeButton 类,需要在 CursorMano.h 中包含以使编译器能够看到 NodeButton 的定义,但是 NodeButton.h 本身包含 CursorMano.h
您需要使用前向声明来打破这种循环依赖性。
NodeButton.h 中,您只需要使用指向 CursorMano 的指针,因此不需要包含 CursorMano.h,只需在使用命名空间声明后进行类的前向声明即可。
using namespace std;
using namespace is;

class CursorMano;

好的,那么我如何在这两个类之间引用彼此? - Pier
1
@Pier:在NodeButton.h中前向声明CursorMano(写入class CursorMano;),并从NodeButton.h中删除#include“CursorMano.h” - Mankarse
啊,就在我打字回答的时候 :) - kevintodisco
@ktodisco:你得到了我的点赞。 - Alok Save

1
这可能是由于两个头文件之间的循环依赖关系导致的(NodeButton 包含 CursorMano,而 CursorMano 又包含 NodeButton)。尝试在 NodeButton.h 中删除 #include "CursorMano.h" 并在 NodeButton 声明前添加 class CursorMano;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接