473,782 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class-in-a-typedef-in-a-class circular trouble

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_v ar;
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.

Jul 19 '05 #1
11 3904

"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_v ar;
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

Jul 19 '05 #2
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.

Jul 19 '05 #3
"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_v ar;
| 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_cla ss,int> would
probably be a good idea...

hth,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #4
Ivan Vecerina wrote:
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];
}

Yes, I think I might try that. I've got it all inside
a (named) namespace already, which I stripped for brevity here.
I guess it won't interfere?
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_cla ss,int> would
probably be a good idea...


It was arbitrary, for it was a simplified example only.
In reality the lookup table would have about a million
elements. The size is known at compile time, and the
look-up table will have to be transferrable between
processors via MPI -- that's why a std::vector is not
a good idea -- it can't be copied "memcpy-wise",
whereas a traditional array can be.

Thanks for the suggestions,
- J.

Jul 19 '05 #5
On Thu, 11 Sep 2003 12:16:49 +0000, Jacek Dziedzic
<jacek@janowo-NOSPAM-.net> wrote:
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_v ar;
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.


Do it the other way around:

struct lookup_record;

class my_class {
private:
int some_internal_v ar;
static lookup_record lookup_table[1000];
public:
static void prepare_lookup_ table();
};

struct lookup_record
{
my_class instance;
int lookup_value;
};

lookup_record my_class::looku p_table[1000];

static members can have incomplete types.

Tom
Jul 19 '05 #6
Hi Jacek,
"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj******** **@korweta.task .gda.pl...
| Ivan Vecerina wrote:
| > 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];
| > }
| >
|
| Yes, I think I might try that. I've got it all inside
| a (named) namespace already, which I stripped for brevity here.
| I guess it won't interfere?
An unnamed namespace may be nested within another namespace, this
won't be a problem (if that's what you meant by "interfere" ).
(it just generates longer identifiers for the linker, no big deal).

| The size is known at compile time, and the
| look-up table will have to be transferrable between
| processors via MPI -- that's why a std::vector is not
| a good idea -- it can't be copied "memcpy-wise",
| whereas a traditional array can be.
Note that an std::vector always allocates contiguous storage,
so the whole contents can be copied by memcpy (if items are POD).
But if the data is strictly fixed-size, no need to bother.

hth, Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #7
Thanks a lot, Ivan! Your namespace idea worked just fine!

- J.

Jul 19 '05 #8
Amazingly simple, that!

thanks,
- J.

Jul 19 '05 #9
Jacek Dziedzic <jacek@janowo-NOSPAM-.net> wrote in message news:<bj******* ***@korweta.tas k.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:
What is struct lookup_record good for? Why do you not put it's member
`int lookup_value' inside my_class?

Try get a clear idea of the responsibilitie s of lookup_record and my_class.
From the code snippet you've given you can not look-up a instance of
my_class without already having a reference to that class!?

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


This is C, in C++ you define a struct exactly the same way as a class.

struct lookup_record {
my_class instance;
int lookup_value;
};

regards, Stephan
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5025
by: ajikoe | last post by:
Hello, I have two modules (file1.py and file2.py) Is that ok in python (without any weird implication) if my module import each other. I mean in module file1.py there exist command import file2 and in module file2.py there exist command import file1? This is not working in C#. pujo
2
3105
by: ernesto basc?n pantoja | last post by:
Hi everybody: I'm implementing a general C++ framework and I have a basic question about circular dependencies: I am creating a base class Object, my Object class has a method defined as: virtual String toString(); where String is defined as:
16
2844
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more semi-circular definitions and
4
8264
by: pnp | last post by:
I'm developing an app (in C #) that uses 2 usercontrols that must be in different dll's. The problem is that each one needs to use the other, so as a result I get a circular reference error when I try to add the references to the dll's. Is there a way round this problem? In C++ one could use header files... What can I do here?
0
3328
by: Alan Samet | last post by:
Before telling me what I already know about what this error means, please read the post. I encountered this bizarre error when running aspnet_compiler.exe. Unfortunately, I don't know of a way to reproduce it outside of the context in which I encountered it, so this post is more to serve as a record and possible workaround for others than may encounter the same issue. There was no circular reference in the control, or through that
7
13525
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time dependencies). But then I started wondering how "easy" it would be to similarly make a NON-RUNTIME circular dependency between (implicitly linked) DLLs. Indeed authors like John Lakos, who focus on compile/link-time dependencies (not run-time),...
3
3745
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I Have a solution with about 50 projects and each project have References to 1 to n of the projects in the solution. I try go to a project and try to add a reference to another project and I get a Circular Reference" error and cannot reference the project. Is there any way to find out the what Method or Project is causing the problem If I am on Project A tring to add Project B - I checked that A doesn't include B and B doesn't...
0
1676
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup glitches in the massive, complex machines they use to smash atoms.
2
1859
by: Dansk | last post by:
Hi all, I am currently writing some code that explores assemblies dependencies. I start loading the first assembly with Assmebly.LoadFrom which gives me an Assembly instance. Then, I enumerate the AssemblyNames from the GetReferencedAssemblies() collection.
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.