xcode C++ 中的未知类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9171307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unknown type error in C++
提问by Pier
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.
突然,当我包含 .h 并写入 var Xcode 给我大量错误......以及未知类型错误。
How can it be unknown when the .h is included right there?
当 .h 包含在那里时怎么会是未知的?
Here is the NodeButton.h file which would correspond to the MyClass.h in the example
这是 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:
};
And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.
这里是 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;
};
回答by Alok Save
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
.
NodeButton.h
定义需要包含的NodeButton
类,CursorMano.h
以便编译器可以看到定义NodeButton
但NodeButton.h
本身包含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.
在NodeButton.h
你只使用一个指针,CursorMano
所以你不需要CursorMano.h
在 using 命名空间声明之后包含just forward 声明类。
using namespace std;
using namespace is;
class CursorMano;
回答by kevintodisco
It's probably a result of the circular dependency between you two header files (NodeButton
includes CursorMano
and CursorMano
includes NodeButton
). Try removing the #include "CursorMano.h"
in NodeButton.h and add class CursorMano;
before your NodeButton
declaration.
这可能是两个头文件(NodeButton
包含CursorMano
和CursorMano
包含NodeButton
)之间循环依赖的结果。尝试删除#include "CursorMano.h"
NodeButton.h 中的 并class CursorMano;
在您的NodeButton
声明之前添加。