| |

VerySource

 Forgot password?
 Register
Search
View: 735|Reply: 4

Find undefined type error solution

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-17 14:30:01
| Show all posts |Read mode
I've asked a question once, but I didn't explain it last time, let me talk about it this time. Masters help to see how to solve.
testA.h:

#if! defined (_a_)
#define _a_
#include "testB.h"
class A {
public:
B b;
public:
aMethod ();
};
#endif

testA.cpp:

#include "testA.h"
A :: aMethod () {

}

testB.h
#if! defined (_b_)
#define _b_
// # include "testA.h" <-This Include is not easy to use
class A;
class B {
public:
A * a;
public:
setA (A * a);
bMethod ();
};
#endif

testB.cpp
#include "testB.h"
B :: setA (A * a) {
this-> a = a;
}
B :: bMethod () {
a-> aMethod ();
}

Compiling class A is fine, compiling class B has errors:
testB.cpp
testB.cpp (6): error C2027: Undefined type "A" was used
        d:\dxsdk\Extras\DirectShow\Samples\C ++\DirectShow\course_client2\destop capture filter\testB.h (4): See the statement of "A"
testB.cpp (6): error C2227: The left side of "-> aMethod" must point to class / structure / union


How should this mutual relationship be resolved.
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-6-19 08:00:01
| Show all posts
testB.cpp
#include "testB.h"
#include "testA.h"
Reply

Use magic Report

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-19 18:15:01
| Show all posts
It’s so simple. I’m dizzy. Can you explain why I didn’t add #include "testA.h" to testB.h?
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-6-19 20:15:01
| Show all posts
Because the loop contains.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-20 19:15:01
| Show all posts
There is only a pointer of class A in class B, then there is a forward declaration of class A when class B is defined,
However, class A uses class B as a member, so when defining class A, you must see the complete definition of class B.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list