473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Parallell struct dependancy in C++

Hi.
I am having trouble with the definition of my two structs in C++. Both
have functions depending on each other, which means none of them can
be declared before the other. How may I solve this?

A quick example:
struct structA{
int x;
void functionA(structB);
}

struct structB{
int y;
void functionB(structA);
}

This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...

Do you know of any possible solutions?

May 8 '07 #1
9 1460
On May 8, 11:54 am, roy...@gmail.com wrote:
Hi.
I am having trouble with the definition of my two structs in C++. Both
have functions depending on each other, which means none of them can
be declared before the other. How may I solve this?

A quick example:
you need a forward declaration here, as detailed in FAQ:
[39.11] How can I create two classes that both know about each other?
http://www.parashift.com/c++-faq-lit...html#faq-39.11
struct structA{
int x;
void functionA(structB);

}
thats not a valid declaration, missing semicolon
>
struct structB{
int y;
void functionB(structA);

}

This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...

Do you know of any possible solutions?
#include <iostream>

class B; // forward declaring an incomplete type

struct A {
int x;
void function( const B& );
};

void A::function( const B& r_b )
{
std::cout << "A::function(const B& r_b)\n";
}

struct B {
int y;
void function( const A& );
};

void B::function( const A& r_a )
{
std::cout << "B::function(const A& r_a)\n";
}

int main()
{
A a;
B b;
b.function( a );
}
May 8 '07 #2
Thanks!
That was it - genius...

All the best.

May 8 '07 #3
On 2007-05-08 17:54, ro****@gmail.com wrote:
Hi.
I am having trouble with the definition of my two structs in C++. Both
have functions depending on each other, which means none of them can
be declared before the other. How may I solve this?

A quick example:
struct structA{
int x;
void functionA(structB);
}

struct structB{
int y;
void functionB(structA);
}

This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...

Do you know of any possible solutions?
Forward declarations and pointers:

You start by telling the compile that there exists a structure named
structB but you don't tell it anything about it (this is the forward
declaration), then you declare structA, but with the modification that
the function takes a pointer to a structB. And finally you declare structB:

struct structB;

struct structA{
int x;
void functionA(structB*);
}; // Dont forget the ;

struct structB{
int y;
void functionB(structA);
};

--
Erik Wikström
May 8 '07 #4
On May 8, 9:32 am, Erik Wikström <Erik-wikst...@telia.comwrote:
I am having trouble with the definition of my two structs in C++. Both
have functions depending on each other, which means none of them can
be declared before the other. How may I solve this?


Forward declarations and pointers:
There's no need to switch to pointers to fix this problem (although
there might be other, unrelated reasons to use pointers).

When you *declare* a method, if the parameters and return types are
pointers, references, or the classes/structs themselves doesn't
matter. You don't need the definitions. You only need the
definitions where you use the class. That would include the method
definitions and the call site.

The simple solution to the OP's problem is this:
1) Declare classes, structs, and their methods in .h files.
2) Define methods in .cpp or .cc files only, not in .h files.
3) Forward declare classes as needed in the .h files.
4) Have the .cpp files #include both .h files.

All the best.
Michael

May 8 '07 #5
This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...

Do you know of any possible solutions?
Use forward declaration like:
class A;
class B;

class A { ... };
class B { ... };

However, you'll also need to use pointer to reference the other
classes.

May 8 '07 #6
On May 8, 10:32 am, xman <cshin...@gmail.comwrote:
>
However, you'll also need to use pointer to reference the other
classes.
Not true. This is a very common misconception (two people have posted
it so far), but it is simply not correct. See my previous post.

Michael

May 8 '07 #7
On May 8, 12:32 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-05-08 17:54, roy...@gmail.com wrote:
Hi.
I am having trouble with the definition of my two structs in C++. Both
have functions depending on each other, which means none of them can
be declared before the other. How may I solve this?
A quick example:
struct structA{
int x;
void functionA(structB);
}
struct structB{
int y;
void functionB(structA);
}
This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...
Do you know of any possible solutions?

Forward declarations and pointers:

You start by telling the compile that there exists a structure named
structB but you don't tell it anything about it (this is the forward
declaration), then you declare structA, but with the modification that
the function takes a pointer to a structB. And finally you declare structB:
There is a certain level of confusion here. Using the pointer (or
better - a reference / const reference) only prevents invoking copy
construction once the function is called. It serves no other purpose.
The use of pointer/reference is only required to prevent circular
redundancy in the case one class is a member of the other and vice
versa.

example:

struct B;
struct A
{
B m_b;
};

struct B
{
A m_a;
};

int main()
{
B b; // the compiler is unable to calculate the size of type B
}

An A has a B which has an A which has a B ... to infinity
That scenario is *not* one that the OP suffers from.
May 8 '07 #8
On May 8, 1:32 pm, xman <cshin...@gmail.comwrote:
This produces an error for structA, because it is missing a
declaration of structB. But, declaring structB before structA, just
turns the problem around as structB then will be missing a declaration
of structA...
Do you know of any possible solutions?

Use forward declaration like:
class A;
class B;

class A { ... };
class B { ... };

However, you'll also need to use pointer to reference the other
classes.
No he doesn't. He can use whatever he finds appropriate for his needs.
The OP is dealing with parameters to member functions, not members of
the class(es). As an example, consider primitives, he may very well
decide not to use a pointer or reference because his tests show better
results are achieved by copying the primitive. Here is an example.

#include <iostream>

struct B;

struct A {
int n;
A(int n_) : n(n_) { std::cout << "A()\n"; }
A(const A& copy)
{
std::cout << "copy A\n";
n = copy.n;
}
void function( const B ); // by value
};

struct B {
int n;
void function( const A ); // by value
};

void A::function( const B b )
{
n = b.n;
}

void B::function( const A a )
{
n = a.n;
}

int main()
{
A a(99);
B b;
b.function( a );
std::cout << b.n << std::endl;
}

/*
A()
copy A // fast copy
99
*/

Again, the only reason to use a pointer or reference or const
reference is if the instance being passed as a parameter has a
performance hit if_and_when a copy ctor is invoked. Thats is not the
Op's situation, he is *not* suffering from circular membership /
redundancy.
May 8 '07 #9
There's no need to switch to pointers to fix this problem (although
there might be other, unrelated reasons to use pointers).
Got it :) I replied before I was able to see this post.

May 9 '07 #10

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

Similar topics

4
by: Trev | last post by:
Is it possible to rebuild dependancy information for objects in a database? I can hardly ever DTS a database across to another server because something will fail with the message "invalid object...
1
by: Ola Sprauten | last post by:
I'm trying to write an app that communicates with a device connected to the parallell port of my PC. In C++ this was easy enough, just use the outb call to write bytes to the specified address. How...
1
by: Christopher | last post by:
I made up a solution. I want to write a .dll and another project to test the ..dll So far I have created the .dll project and the test .exe project in the same solution. I set the .dll project as...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
1
by: ker chee huar | last post by:
Hi all! i am using Dotfuscator Community Edition for encrypt my .Net assembly. How can i include all my dependancy Dll to encrypt it because my project is multi-reference project! regards,...
3
by: Johan Johansson (Sweden) | last post by:
I wonder why it is that no more than 10 clients can access a webservice at a time, and how to increase that number. The eleventh client get a Http 403.9 (Access forbidden : To many users are...
1
by: Learnicus | last post by:
Hello, I have a Winforms app that accessess a webservice application that i have written. Both of these use a common class called replication to carry changes from an online database <> offline...
3
by: KenL | last post by:
I have a WinForm(VB 2.0) application that I am publishing as a ClickOnce installation. We have a need to publish the same application code for several configurations. (Staging, TestSystem,...
1
by: Dave | last post by:
Is it possible to build an MS sql 2005 caching dependancy stucture similar to what is offered by the SqlCacheDependency class for use with non asp.net applications using MS sql 2005 notification...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.