我有一个字符串类,毫不奇怪,它使用不同的实现,具体取决于是否启用了UNICODE.
#ifdef UNICODE
typedef StringUTF16 StringT;
#else
typedef StringUTF8 StringT;
#endif
Run Code Online (Sandbox Code Playgroud)
这很好用但我目前有一个问题,我需要转发声明StringT typedef.我怎样才能做到这一点?
我不能这样做typedef StringT;,这使得前瞻性声明变得棘手.是否可以执行此typedef类型的前向声明,而不必将上面的代码放到头文件的顶部?
我在命名空间中有一组类,并希望在define.hpp文件中对转发声明进行分组.
我已经在其他项目中实现了这一点,这是一个例子:
namespace Makefile
{
class Builder;
class Config;
class Options;
class Target;
class Tool;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 例如 - Config类包含以下强类型枚举:
namespace Makefile
{
class Config
{
public:
enum class OperatingSystem : unsigned int
{
MacOSX = 0,
Linux = 1,
Windows = 2
};
};
}
Run Code Online (Sandbox Code Playgroud)
所以我想在我的前向声明文件中添加这个枚举类,并添加以下行:
enum class Config::OperatingSystem : unsigned int;
Run Code Online (Sandbox Code Playgroud)
这是我的问题,编译时出现以下错误:
src/Makefile/define.hpp:6:13: error: opaque-enum-specifier must use a simple identifier
Run Code Online (Sandbox Code Playgroud)
这是否意味着无法转发声明嵌套类型?这个问题有解决方法吗?这是由于我的编译器吗?
我的配置:Mac OS X.7上的GCC 4.6.0
编译标志: --std=c++0x -g3 -gdwarf-2 -W -Wall -iquote gen -iquote src
我的标题看起来像这样:
namespace Dummy
{
ref class ISYSession {};
namespace Afw
{
/// <summary>Sammlung von AFW-Utility-Methoden</summary>
public ref class AfwUtility
{
public:
static void CopyAFWParamsToIDictionary(AFWParams ¶meterIn, System::Collections::IDictionary^ parameterOut);
static AFWParams* CopyIDictionaryToAFWParams(System::Collections::IDictionary^ dictionary);
static void ShowExceptionLog(System::String^ sessionId);
static void ShowStatementLog(System::String^ sessionId);
static Dummy::ISYSession^ GetSession(AFWAppClass *acl);
};
}
}
Run Code Online (Sandbox Code Playgroud)
如果我不使用我的ref类的标题,我不能在同一个程序集中使用它.但是使用这个头文件我的代码不再编译了.
这是前两个错误:
c:\ develop ...\xy.dll:警告C4944:'ISYSession':Das Symbol kann nicht aus'c:\ develop ...\xy.dll'importiert werden:'Dummy :: ISYSession'isre bereits im aktuellen Bereich vorhanden.
(英语:"'Dummy :: ISYSession':无法从xy.dll导入符号:Dummy :: ISYSession已存在于当前范围内.")
错误C3699:"^":Diese Referenzierung kannnichtfürdenTyp"Schleupen :: CS :: SY :: ISYSession"verwendet werden. …
我有这样的协议:
#import <Foundation/Foundation.h>
@protocol Prot1 <NSObject>
@required
- (void)methodInProtocol;
@end
Run Code Online (Sandbox Code Playgroud)
这是我想要存储在这样的类中的委托的协议:
#import <Cocoa/Cocoa.h>
@class Prot1;
@interface Class1 : NSObject
@property (nonatomic, strong) Prot1 *delegate;
- (void)methodInClass;
@end
Run Code Online (Sandbox Code Playgroud)
这个类的实现是这样的:
#import "Class1.h"
#import "Prot1.h"
@implementation Class1
@synthesize delegate;
- (void)methodInClass {
[delegate methodInProt];
}
@end
Run Code Online (Sandbox Code Playgroud)
当我构建这些代码时,我收到以下错误:
Receiver type 'Prot1' for instance message is a forward declaration
Run Code Online (Sandbox Code Playgroud)
这有什么不对?我确实理解我必须通过@class为协议做一个前向声明,我认为我只需要#import协议,在类实现中......不是吗?
假设我有一些模板类前向声明,我想键入一个共享指针.我该怎么做?
template<typename T> class Arg;
typedef std::tr1::shared_ptr<Arg> ArgPtr; // Compiler error
Run Code Online (Sandbox Code Playgroud) 我不能继承只是前向声明的结构吗?
例:
struct A;
class B : public A {};
Run Code Online (Sandbox Code Playgroud)
我收到错误"基类未定义".
你能帮我解决这个问题吗?
谢谢
我试图在我的c ++程序中包含文件,但我一直遇到错误:
ShapeVisitor.h:9:28: error: ‘Circle’ has not been declared
我认为问题在于类的结构方式,它导致循环依赖.我该如何解决这个问题?
类标题如下......
//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
// headers, ...
#include "Shape.h"
class Circle: public Shape {
//class declaration
}
#endif
//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
// headers, ...
#include <iostream>
class Shape {
//a certain method in the class declaration looks like this
virtual void accept(ShapeVisitor& v) = 0;
//rest of class
}
#endif
//ShapeVisitor.h
#ifndef SHAPEVISITOR_H
#define SHAPEVISITOR_H
#include "Circle.h"
class ShapeVisitor {
//a method in the class looks …Run Code Online (Sandbox Code Playgroud) 查看OpenCL头文件,我看到:
typedef struct _cl_context * cl_context;
Run Code Online (Sandbox Code Playgroud)
我知道这cl_context是一个指向前向声明结构的指针_cl_context.
从图书馆设计师的角度来看,这样做有什么好处:
typedef struct _cl_context cl_context;
Run Code Online (Sandbox Code Playgroud)
这样的API调用可以cl_context代替cl_context*吗?但如果是这样,为什么不这样做:
typedef void *cl_context;
Run Code Online (Sandbox Code Playgroud) 我知道我们应该更倾向于在头文件中声明所有内容,如果可能的话,那么STL呢?
我发现iostream有iosfwd.
如果我想在我的班级中声明一个互斥锁,如下所示:
class MyClass
{
.....
private:
std::mutex mMutex;
};
Run Code Online (Sandbox Code Playgroud)
我应该在我的类标题中包含互斥标头吗?或者有没有办法向前推进它,例如:
class std::mutex;
class MyClass{...};
Run Code Online (Sandbox Code Playgroud)
对于计时器和线程也是如此.
对此的任何想法都是适用的.谢谢!
我真的很喜欢在课程开始时声明我的所有方法,并希望通过前向声明来实现,然后再进一步实现它们.这可能在C#中吗?
例如:
private void Test();
private void Test()
{
}
Run Code Online (Sandbox Code Playgroud)