473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to solve this problem?

Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.

Nov 21 '06 #1
16 2090
ClassA.h should not include the ClassB.h
"Hooyoo дµÀ£º
"
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
Nov 21 '06 #2
I know in these similar codes ClassA.h no need to include classb.h,
but in my real project I need do this.
Can I solve this problem in other ways?
Äжù¶à±¡ÐÒ wrote:
ClassA.h should not include the ClassB.h
"Hooyoo дµÀ£º
"
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
Nov 21 '06 #3

"Hooyoo дµÀ£º
"
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"
Here, you include "ClassA.h" in "ClassB.h", and include "ClassB.h" in
"ClassA.h". I am not quite clear about the link issues.Maybe some one
could explain it more clearly.
But I am sure that there is only one "include" works, and it seems to
be the include "ClassB.h" in "ClassA.h", as you declare "class ClassA"
below.
class ClassA;
Here, you pre-declare the classA.It just tell the compile that ClassA
is a class defined somewhere, so you can declare instances of ClassA in
this file scope.But ClassB doesn't know anything about ClassA.
class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
When you define the member function Dosometing, you just know ClassA is
a class but nothing more. Access to any member of the classA will be
undefined.When the function takes place, the compile does not know how
to create a temporary object of ClassA, as "it was not defined".
You could use ClassA * here just for compile, but you still can't
access any member of ClassA through the pointer.
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
Nov 21 '06 #4

Hooyoo wrote:
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
no_dot_h headers like to go with #ifndef-#define-#endif

Nov 21 '06 #5
Compiler job do it, because it don't recognise what to compile first
giving some file, by what you say, ".h" files. Also, i think always
writing 'public' to indicate what is publicised is good too. That
increase readable.

Best,
Peter Liu
cppcoder wrote:
Hooyoo wrote:
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.

no_dot_h headers like to go with #ifndef-#define-#endif
Nov 21 '06 #6
That's not really true, #defien is kewords originally from C not C++.
header files u say are c++ not c. there is no such things as #ifndef
definetion in C++.

-Ben Howards
~FPCP=for potatoes chip pickers~

cppcoder wrote:
Hooyoo wrote:
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.

no_dot_h headers like to go with #ifndef-#define-#endif
Nov 21 '06 #7
* Ben Howards:
[top-posting].
Please don't -- see FAQ item 5.4.

Besides, what you wrote is incorrect.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 21 '06 #8

Hooyoo wrote:
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;
remove the above line. ClassA is already defined in ClassA.h
You should use #ifndef, #define and #endif directives if you want to be
portable (instead of pragma once).
>
class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"
Remember, compilers don't know that header files exist at compilation
time.
To explain the issue, the compiler injects the ClassB.h contents here
in replacement of the #include directive during precompilation. And if
you look closely at that header, it in turn injects ClassA.h before the
declaration and definition of Class B.

So you want to end up with:

ClassA {...};
ClassB {...};
int main() { ... }

If you don't remove the forward declaration of ClassA, you get this:

ClassA {...};
ClassA;
ClassB {...};
int main() { ... }

which explains the error below... the redeclaration hides the previous
definition.
>
int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
Nov 21 '06 #9
Hooyoo,

You must move what is common for both files to a third one, and then
include this third one to both of your original files.

Hooyoo wrote:
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
Nov 21 '06 #10

"Hooyoo" <zh*********@126.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...

Please don't top-post. Put your replies at the end, or interspersed with
what you're replying to. (I've re-arragned the converstaion below.)
>

"Hooyoo P45@#:
"
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"
Why do you need to include that header?
class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

class ClassA;
Why are you forward-declaring ClassA and ALSO including its header? Do one
or the other.

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}
Why do you include a file but never use it?

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
DP6y6`1!PR wrote:
ClassA.h should not include the ClassB.h
I know in these similar codes ClassA.h no need to include classb.h,
but in my real project I need do this.
Why? Show us code which actually represents the problem you're having. The
above code does not.
Can I solve this problem in other ways?
Yes, I'm sure you can, but we don't know what the problem is.

You might want to look at the FAQ: http://www.parashift.com/c++-faq-lite/

Or do a Google Search for "C++ circular dependency".

-Howard


Nov 21 '06 #11

"Howard" <al*****@hotmail.comwrote in message
news:1q*******************@bgtnsc05-news.ops.worldnet.att.net...
>
"Hooyoo" <zh*********@126.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...

Please don't top-post. Put your replies at the end, or interspersed with
what you're replying to. (I've re-arragned the converstaion below.)
Looks like I re-arranged a few letters there! I think my fingers are
dyslexic. :-)

Should be:
"re-arranged the conversation"

-Howard
Nov 21 '06 #12

mimi wrote:
"Hooyoo дµÀ£º
"
Following are similar codes of my project:
At first I define two classes in two files:
//ClassA.h
#pragma once
#include "ClassB.h"

class ClassA
{
public:
ClassA(void){};
public:
~ClassA(void){};
};

//ClassB.h
#pragma once
#include "ClassA.h"

Here, you include "ClassA.h" in "ClassB.h", and include "ClassB.h" in
"ClassA.h". I am not quite clear about the link issues.Maybe some one
could explain it more clearly.
But I am sure that there is only one "include" works, and it seems to
be the include "ClassB.h" in "ClassA.h", as you declare "class ClassA"
below.
class ClassA;

Here, you pre-declare the classA.It just tell the compile that ClassA
is a class defined somewhere, so you can declare instances of ClassA in
this file scope.But ClassB doesn't know anything about ClassA.
class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};

When you define the member function Dosometing, you just know ClassA is
a class but nothing more. Access to any member of the classA will be
undefined.When the function takes place, the compile does not know how
to create a temporary object of ClassA, as "it was not defined".
You could use ClassA * here just for compile, but you still can't
access any member of ClassA through the pointer.
};
Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}

When I complie these codes in VS2005, I get 1 error:
Error 1 error C2027: use of undefined type 'ClassA' d:\v-james\test
code\consoledemo\consoledemo\classb.h 12

So, somebody here please say something about this, thanks.
Thanks to all guys. But I think only mimi really understand me, other
guys totally lost, but still thank you.

Nov 22 '06 #13

"Hooyoo" <zh*********@126.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Thanks to all guys. But I think only mimi really understand me, other
guys totally lost, but still thank you.
Well, if we were lost, it's because you didn't post "real" code, and didn't
follow up with answers to our questions. Don't blame us if we can't read
your mind.
Nov 27 '06 #14
If I'm not mistaken, you can solve this problem doing the following.
Note that no extra files are included in ClassA.h and ClassB.h
// File: ClassA.h
class ClassB; // Declaration of ClassA so that ClassB knows it exists
class ClassA
{
// something to do with ClassB
};

------------------------------------------------

// File: ClassB.h
class ClassA; // Declaration of ClassB so that ClassA knows it exists
class ClassB
{
// something to do with ClassA
}

------------------------------------------------

// File: ClassA.cpp
#include "ClassA.h"
#include "ClassB.h"

-------------------------------------------------

// File: ClassB.cpp
#include "ClassA.h"
#include "ClassB.h"

The limitation of this is that you can't access members of ClassA in
ClassB.h, or ClassB in ClassA.h. You can, however, use ClassA.cpp and
ClassB.cpp to do anything you need.

I have not tested this with anything more than a few pointers. (i.e
ClassA * pClassA). I'm not sure if it will let you use non-pointer
members to cross-referenced classes.

-- Tristan.

Nov 29 '06 #15
Hi!

I think that the only one who realy touched the real problem was
Howard. Hooyoo, you did created a CYCLYC dependency between files!!!
That's the problem! A solution would be to use pointers and references
into one of the headers and to elimnate the need to include the header
itself. In that case the compilator won't ask you about the class
definition. It suffices the declaration. Thus, try to have, let's say
in header of classA, only pointers and references of type classB (every
method of classA that was previously inline and made calls to classB
goes into .cpp):

//ClassA.h
#pragma once
//declare classB
class classB;

class ClassA
{
public:
ClassA(void) : cB ( 0 ){};
const classB & func1( classB *) ;
void funct2(const classB & );

public:
~ClassA(void){};

private:
classB *cB;
};
Now, everything that is releated to the implementation of B goes into
..cpp:

//ClassA.cpp
#include "ClassA.h"
#include "ClassB.h" // HERE YOU CAN INCLUDE classB

const classB & ClassA::func1( classB *){...}
void ClassA::funct2(const classB & ) {...}
The header of B could remain the same:

//ClassB.h
// #pragma once
#include "ClassA.h"

//!!!!!!!!! it doesn't matter if you declare the class after its
definition
class ClassA;

class ClassB
{
public:
ClassB(void){};
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
};

Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}


enjoy,
dreqEu.

Nov 29 '06 #16

tetra wrote:
Hi!

I think that the only one who realy touched the real problem was
Howard. Hooyoo, you did created a CYCLYC dependency between files!!!
That's the problem! A solution would be to use pointers and references
into one of the headers and to elimnate the need to include the header
itself. In that case the compilator won't ask you about the class
definition. It suffices the declaration. Thus, try to have, let's say
in header of classA, only pointers and references of type classB (every
method of classA that was previously inline and made calls to classB
goes into .cpp):

//ClassA.h
#pragma once
//declare classB
class classB;
classB does not exist
>
class ClassA
{
public:
ClassA(void) : cB ( 0 ){};
remove semicolon above.
const classB & func1( classB *) ;
classB does not exist.
void funct2(const classB & );

public:
~ClassA(void){};

private:
classB *cB;
classB does not exist.
};
Now, everything that is releated to the implementation of B goes into
.cpp:

//ClassA.cpp
#include "ClassA.h"
#include "ClassB.h" // HERE YOU CAN INCLUDE classB

const classB & ClassA::func1( classB *){...}
same, there is no classB, only ClassB
void ClassA::funct2(const classB & ) {...}
The header of B could remain the same:

//ClassB.h
// #pragma once
#include "ClassA.h"

//!!!!!!!!! it doesn't matter if you declare the class after its
definition
class ClassA;

class ClassB
{
public:
ClassB(void){};
remove the semicolons
public:
~ClassB(void){};
void DoSomething(ClassA obj){};
remove the semicolon above
};

Then main function:
//main.cpp
#include "ClassB.h"

int main(void)
{
return 0;
}
The correct way to forward declare would be as follows, note that
templates would do away with the problems at hand:

// ClassA.h
#ifndef CLASSA_H_
#define CLASSA_H_

#include "ClassB.h"
class ClassB;

class ClassA {
ClassB* p_B;
public:
ClassA( void ) : p_B(0) { }
~ClassA() { }
const ClassB& func1() const;
void funct2(const ClassB &);
};
#endif

//ClassA.cpp
#include "ClassA.h"

const ClassB& ClassA::func1() const
{
return *p_B;
}
void ClassA::funct2(const ClassB& r_b)
{
// ...
}

// ClassB.h
#ifndef CLASSB_H_
#define CLASSB_H_

#include "ClassA.h"
class ClassA;

class ClassB {
public:
ClassB() { }
~ClassB() { }
void DoSomething( const ClassA& r_a ) { }
};
#endif

// test.cpp
#include "ClassB.h"

int main()
{
ClassB instance;
return 0;
}

Although i'ld rather *.hpp headers over *.h headers.

Nov 29 '06 #17

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

Similar topics

1
by: BVM | last post by:
Hi, All: I have this error. It seems execution time is too long. Actually the execution time is about 30 seconds(I tested in Query analyzer). How do I solve this problem? ...
3
by: Alex | last post by:
Hi, I have a problem involving some design issue. I have two unrelated (that is, they do not derive from the same base) classes: ClassA ClassB Both have a quite similar interface, so they can...
7
by: Shapper | last post by:
Hello, I have an ASP:ImageButton where I want to call a function and pass a string: OnClick="Change_Photo("John")" I am having problems with "". I tried
6
by: Federico | last post by:
Hi, this is what I can do: - Create new solutions using VS.Net ASP.Net - Save the solutions, build the solution, view in browser with the solution still open. But, once I close the solution, I...
0
by: Jitesh | last post by:
I am facing a problem in webservice, I want to know what will be the exact procedure to solve the problem............. What I want to do............ I have a table named order in SQL Server....
27
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
17
by: Michael Reichenbach | last post by:
Here is the example code. int main(int argc, char *argv) { string Result; WIN32_FIND_DATA daten; HANDLE h = FindFirstFile(TEXT("c://test"), &daten); system("PAUSE"); return EXIT_SUCCESS; }
2
by: itsvineeth209 | last post by:
My task is to create login control without using login control in tools. I shouldnt use sqldatasource or any other. I should use only data sets, data adapters and data readers etc. U had created...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.