class-in-a-typedef-in-a-class循环问题 [英] class-in-a-typedef-in-a-class circular trouble

查看:92
本文介绍了class-in-a-typedef-in-a-class循环问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我正在创建一个具有静态方法的类,它将计算一个查找表,该表将在以后由所有成员计算出来。类。

问题是,查找表由记录组成,一个记录字段的
是类本身,即我的目标是

类似于:


typedef struct {

my_class instance;

int lookup_value;

} lookup_record;


class my_class {

private:

int some_internal_var;

static lookup_record lookup_table [1000];

public:

static void prepare_lookup_table();

// ...

};


但是不能编译my_class用作类型但是

未定义为类型。那么,编译器还没有知道
,my_class是什么。所以我试着添加一行


class my_class;


就可以了,但之后它说field my_class has

不完整类型。如果我尝试将typedef放在

类之后,那么类声明不知道

typedef是什么。类和typedef也不接受extern。


我该怎么办?顺便说一句,这就是头文件。


tia,

- J.

解决方案



" Jacek Dziedzic" < jacek@janowo-NOSPAM-.net>在消息中写道

news:bj ********** @ korweta.task.gda.pl ...

你好!
<我正在创建一个具有静态方法的类,该方法将计算该类所有成员稍后需要的查找表。
问题是,查找表由记录和记录字段中的一个是类本身,即我的目标是:

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table [1000];
public :
static void prepare_lookup_table();
// ...
};

但不能用my_class编译用作类型但是没有定义为类型。那么,编译器还没有知道,my_class是什么。所以我试着添加一行

类my_class;




你需要告诉编译器类的大小。

如何通过查看前向声明来推断大小。

所以解决问题的一种方法是在你的
$中存储指向my_class的指针b $ b struct而不是对象本身。

my_class * pInstance;。

这样编译器只能使用前向声明(iewithout
$ b) $ b看到它的实际定义)


HTH,

J.Schafer

Josephine Schafer写道:


你需要告诉编译器类的大小。
如何通过查看前进来推断出大小宣言。


我认为前向声明会告诉编译器

在其他地方寻找精确的类声明和

计算大小,但似乎不是*那个*聪明,对吧?

所以解决问题的一种方法是在你的
结构中存储一个指向my_class的指针对象本身。
my_class * pInstance;
这样编译器只能使用前向声明(iewithout
看到它的实际定义)




是的,我想到了这一点,但我认为我无法承受这样的内存和速度上的开销(这是一张查询表)应该

来加快速度,毕竟将由

引入一次额外的解除引用。我想我会忘记上课

整洁并在查找记录中存储内部类变量(一个int)

而不是存储类本身。 />
这看起来不太好,但会有效。


我希望找到一个符合extern class的解决方案,

一个会告诉编译器在其他地方寻找精确的

类定义(稍后在代码中)。


谢谢,

- J.


" Jacek Dziedzic" < jacek@janowo-NOSPAM-.net>在消息中写道

news:bj ********** @ korweta.task.gda.pl ...

|我正在创建一个具有静态方法的类,它将是b $ b |计算该类所有成员稍后需要的查找表。

|问题是,查找表由记录和一个

|组成记录字段是类本身,即我的目标是

|类似于:

|

| typedef struct {

| my_class实例;

| int lookup_value;

| } lookup_record;

|

| class my_class {

|私人:

| int some_internal_var;

| static lookup_record lookup_table [1000];

|公众:

| static void prepare_lookup_table();

| // ...

| };

|但是不能编译[....]


建议:

- 在你的.cpp文件中使lookup_table成为静态全局,

而不是my_class的私有静态成员。

或者更好,在.ccp文件中使用匿名命名空间

将包括lookup_record的定义

和lookup_table:

名称空间{

typedef struct {.....} lookup_record;

lookup_record lookup_table [1000];

}


或者,更改查找表的类型:

static lookup_record * lookup_table ;

(正如Josephine建议的那样)

然后你需要在初始化

函数中分配表,使用new []:

lookup_table = new lookup_record [1000];

....并且在

程序退出之前释放它可能是个好主意:delete [] lookup_table;

第一个解决方案是更好/更简单的IMO。


这就是说,这是固定的大小表(1000个元素)似乎

任意。将查找表的类型更改为

std :: vector< my_class>或者std :: map< my_class,int>将

可能是一个好主意...


hth,

Ivan

-
http://ivan.vecerina.com

Hello!

I''m creating a class that has a static method which would
compute a lookup table needed later by all members of the class.
The problem is, the lookup table is composed of records and one
of the record fields is the class itself, i.e. I''m aiming at
something like:

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_table();
// ...
};

but that won''t compile with "my_class is used as a type but is
not defined as a type". All right then, the compiler doesn''t
know yet, what my_class is. So I tried to add a line with

class my_class;

above it all, but then it says that "field my_class has
incomplete type". If I try to put the typedef after the
class, then the class declaration does not know what the
typedef is. Also classes and typedefs won''t accept extern.

What do I do? This all is, btw, in a header file.

tia,
- J.

解决方案


"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...

Hello!

I''m creating a class that has a static method which would
compute a lookup table needed later by all members of the class.
The problem is, the lookup table is composed of records and one
of the record fields is the class itself, i.e. I''m aiming at
something like:

typedef struct {
my_class instance;
int lookup_value;
} lookup_record;

class my_class {
private:
int some_internal_var;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_table();
// ...
};

but that won''t compile with "my_class is used as a type but is
not defined as a type". All right then, the compiler doesn''t
know yet, what my_class is. So I tried to add a line with

class my_class;



You need to tell the compiler the size of the class also then.
How is to deduce the size just by looking at a forward declaration.
So one way to solve your problem is to store a pointer to my_class in your
struct instead of the object itself.
my_class *pInstance;.
This way compiler can live just with the forward declaration (i.e.without
seeing it''s actual definition)

HTH,
J.Schafer


Josephine Schafer wrote:


You need to tell the compiler the size of the class also then.
How is to deduce the size just by looking at a forward declaration.
I thought that a forward declaration would tell the compiler to
"look somewhere else for the precise class declaration and
compute the size", but it seems it''s not *that* smart, right?
So one way to solve your problem is to store a pointer to my_class in your
struct instead of the object itself.
my_class *pInstance;.
This way compiler can live just with the forward declaration (i.e.without
seeing it''s actual definition)



Yes, I thought about this, but I don''t think I can afford such
overhead in memory and in speed (it''s a look-up table supposed
to speed things up, after all) that would be introduced by
one extra dereferencing. I think I''ll just forget about class
neatness and store the internal class variable (an int)
in the lookup record instead of storing the class itself.
That won''t look good, but would be effective.

I was hoping for a solution along the lines of "extern class",
one that would tell the compiler to look for the precise
class definition somewhere else (later in the code).

thanks,
- J.


"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...
| I''m creating a class that has a static method which would
| compute a lookup table needed later by all members of the class.
| The problem is, the lookup table is composed of records and one
| of the record fields is the class itself, i.e. I''m aiming at
| something like:
|
| typedef struct {
| my_class instance;
| int lookup_value;
| } lookup_record;
|
| class my_class {
| private:
| int some_internal_var;
| static lookup_record lookup_table[1000];
| public:
| static void prepare_lookup_table();
| // ...
| };
| but that won''t compile [....]

Suggestion:
- Make lookup_table a static global in your .cpp file,
instead of a private static member of my_class.
Or better, use an anonymous namespace in the .ccp file
that will include the definition of both lookup_record
and the lookup_table:
namespace {
typedef struct { ..... } lookup_record;
lookup_record lookup_table[1000];
}

Alternatively, change the type of the look-up table:
static lookup_record* lookup_table;
(as Josephine suggested)
You then need to allocate the table in your initialization
function, using new[]:
lookup_table = new lookup_record[1000];
.... and it might be a good idea to free it before
program exit: delete[] lookup_table;
The first solution is nicer/simpler IMO.

This said, this fixed size table (1000 elements) seems
arbitrary. Changing the type of the look-up table to
std::vector<my_class> or std::map<my_class,int> would
probably be a good idea...

hth,
Ivan
--
http://ivan.vecerina.com


这篇关于class-in-a-typedef-in-a-class循环问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆