我有类似以下代码:
class B
{
}
class A
{
enum {
EOne,
ETwo
} EMyEnum;
B myB;
}
Run Code Online (Sandbox Code Playgroud)
我想在类B中声明一个类型为EMyEnum的成员(在A之前声明).这可能吗?我意识到解决方案是将B类声明为秒,但为了清楚起见,我宁愿不这样做.
我如何FILE *在C中转发声明?我通常这样做struct MyType;,但自然这似乎不可能.
如果C标准或编译器与C++之间的行为不同,这也是有意义的.
为什么我要把这个放在一边:我要问的是如何转发声明一个非struct /"typedef'd结构"类型,以便我可以声明指向它的指针.显然void *在源文件中使用和转换它有点hackish.
对我来说,这些术语在使用C编程语言时基本上是同义词.在实践中,我可能更喜欢文件内原型的"前向声明"而不是通过头文件包含的原型的"函数原型".但是当你考虑预处理后发生的事情时,即使这是一个人为的区别.也许我错过了一些东西.
关于何时使用一个术语而不是另一个术语是否存在共识?
我有以下头文件:
https://gist.github.com/wemakeweb/5501443 并且编译器始终报告"未知类型名称类".我已经包括了前进宣言,打破了通告,包括我认为必须的地方.我忘记了什么?
编辑:我把它全部放在一个头文件中,编译器仍然报告"预期;在顶级声明器之后"
https://gist.github.com/wemakeweb/5583500
编辑2 现在我收到链接器错误."架构x86_64的未定义符号"
解决了,问题是
我对一些我认为简单的事情有一个非常奇怪的错误.
#import <Foundation/Foundation.h>
#import "ViewController.h"
#import "GameObject.h"
@interface GameController : NSObject
@property (strong) GLKBaseEffect * effect;
@property (strong) NSMutableArray * gameObjects;
@property (strong) NSMutableArray * objectsToRemove;
@property (strong) NSMutableArray * objectsToAdd;
+ (GameController *) sharedGameController;
- (void) tick:(float)dt;
- (void) initializeGame: (ViewController*) viewcontroller;//ERROR: EXPECTED A TYPE
- (void) createObject:(Class) objecttype atPoint:(CGPoint)position;
- (void) deleteObject:(GameObject*) object atPoint:(CGPoint)position;
- (void) manageObjects;
@end
Run Code Online (Sandbox Code Playgroud)
为什么会问'ViewController'是否是一个类型?这是我正确实施的课程.它也被导入了.
编辑*
如果它有帮助,这是ViewController.m类.
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[GameController sharedGameController] initializeGame:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
编辑2 ** …
import objective-c circular-dependency header-files forward-declaration
我在类的C++前向声明中发现了一个错误,它被错误地声明为struct Book而不是class Book.我认为Book曾经是一个结构,变成了一个类,但前向声明仍然存在.
Book.h:
class Book {
...
};
Run Code Online (Sandbox Code Playgroud)
Library.h:
struct Book;
class Library {
std::vector<Book*> books;
};
Run Code Online (Sandbox Code Playgroud)
没有编译器警告,结果程序似乎工作正常.这让我很好奇:理论上,这会不会引起问题?它只是一个指针,是的,但是,例如,如果类有虚方法,多继承,指针可能不同吗?
我知道关于默认公共/私有的类/结构之间的区别,但我特别询问前向声明和交换它们的可能后果.
我正在尝试创建正确的头文件,其中不包含太多其他文件以保持其清洁并加快编译时间.
这样做时遇到两个问题:
基类的前向声明不起作用.
class B;
class A : public B
{
// ...
}
Run Code Online (Sandbox Code Playgroud)关于STD类的前向声明不起作用.
namespace std
{
class string;
}
class A
{
string aStringToTest;
}
Run Code Online (Sandbox Code Playgroud)我该如何解决这些问题?
我试着看看Stackoverflow和谷歌上列出的类似问题,但他们主要处理模板,这不是我的情况.我在Debian测试64位上使用GCC 4.4.5.
所以,我有两个类 - CEntity:
#ifndef CENTITY_H_INCLUDED
#define CENTITY_H_INCLUDED
#include "global_includes.h"
// game
#include "CAnimation.h"
#include "Vars.h"
#include "vector2f.h"
#include "Utils.h"
class CAnimation;
class CEntity
{
public:
CEntity();
virtual ~CEntity();
void _update(Uint32 dt);
void updateAnimation(Uint32 dt);
void addAnimation(const std::string& name, CAnimation* anim);
void addAnimation(const std::string& name, const CAnimation& anim);
void removeAnimation(const std::string& name);
void clearAnimations();
bool setAnimation(const std::string& name);
SDL_Surface* getImage() const;
const vector2f& getPos() const;
const vector2f& getLastPos() const;
F getX() const;
F getY() const;
F getLastX() const;
F …Run Code Online (Sandbox Code Playgroud) 我头上有很多问号.我没有得到的是在xcode 4.3之前我需要在我的实现文件中声明前向声明(对于私有方法).
就像我的.m文件一样:
// deleting this with xcode 4.3 the below code still does work
// in previous versions i had to put this because otherwise the compiler can't find methodFirst
@interface DetailViewController ()
- (void)methodFirst;
- (void)methodSecond;
@end
@implementation DetailViewController
- (void) methodSecond
{
// if i delete the forward declaration now adays i dont get a compiler error that he cant find method first
[self methodFirst];
}
- (void) methodFirst
{
}
@end
Run Code Online (Sandbox Code Playgroud)
现在看来我不再需要那样做了?Apple是否更新了编译器以便不再需要提交声明?
我找不到任何有关此更改的Apple官方消息来源的参考.我想知道其他人在新环境中遇到了什么.
我有一个关于以下错误的问题" Unknown type name VistaDereIzq"
我有一个叫做的观点VistaDereIzq.我执行以下操作以在此视图中添加该视图.
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "VistaDereIzq.h"
#import "ViewController.h"
@interface VistaNavegador : UIViewController <UIWebViewDelegate>
{
VistaDereIzq *VistaIzq2; <----- "Unknown type name VistaDereIzq"
}
@end
Run Code Online (Sandbox Code Playgroud) c++ ×6
c ×2
class ×2
gcc ×2
objective-c ×2
xcode ×2
c89 ×1
declaration ×1
enums ×1
forward ×1
header-files ×1
import ×1
ios ×1
nested ×1
struct ×1
terminology ×1