I wrote a corresponding error (in fact, it is not easy)
The error is as follows:
1> D: \ work \ win32project \ testeachotherclude \ test2.h (9): Error c2143: syntax error: ";" missing (before)
1> D: \ work \ win32project \ testeachotherclude \ test2.h (9): Error c4430: The type descriptor is missing-it is assumed to be Int. Note: C ++ does not support default int
1> D: \ work \ win32project \ testeachotherclude \ test2.h (9): Error c4430: The type descriptor is missing-it is assumed to be Int. Note: C ++ does not support default int
1> D: \ work \ win32project \ testeachotherclude \ test2.h (10): Error c2061: syntax error: identifier "test1"
1> D: \ work \ win32project \ testeachotherclude. cpp (15): Error c2660: "Test2: classname": the function does not accept one parameter.
1> D: \ work \ win32project \ testeachotherclude. cpp (16): Error c2039: "test1": Not a member of "Test2"
1> D: \ work \ win32project \ testeachotherclude \ test2.h (7): see the "Test2" Statement
1> D: \ work \ win32project \ testeachotherclude. CPP (16): Error c2227: "-> refme" must point to the class/structure/Union/generic type on the left.
Upload test for your convenience:
Main. cpp
#include "stdafx.h"#include "Test1.h"#include "Test2.h"int _tmain(int argc, _TCHAR* argv[]){Test1* t1=new Test1();Test2* t2=new Test2();t1->className(t2);t1->test2->refMe();t2->className(t1);t2->test1->refMe();return 0;}
Let's take a look at test1's
Test1.h
#ifndef Test1_H#define Test1_H#include "Test2.h"class Test1{public:Test2* test2;void Test1::className(Test2* test2);void refMe();};#endif;
Test1.cpp
#include "Test2.h"void Test1::className(Test2* test2){this->test2=test2;std::cout<<"Test1 ";}void Test1::refMe(){std::cout<<"refMe Test1"<<std::endl;}
Test2.h
#ifndef Test2_H#define Test2_H#include "Test1.h"class Test2{public:Test1* test1;void className(Test1* test1);void refMe();};#endif
Test2.cpp
#include "StdAfx.h"#include "Test2.h"#include <iostream>void Test2::className(Test1* test1){this->test1=test1;std::cout<<"Test2 ";}void Test2::refMe(){std::cout<<"refMe Test2"<<std::endl;}
Then run the command to report a pair of errors...
Take a closer look and find that both header files are included in each other. This is the problem... so how can we solve this problem? upload my code and read it to make it easier to understand that main. cpp remains unchanged.
Test1.h
# Ifndef test1_h # define test1_h // # include "test2.h" // use the following method to replace Class Test2; // Note: It only tells the compiler that this class is required, none of the other functional structures include class test1 {public: Test2 * Test2; void test1: classname (Test2 * Test2); void refme () ;}; # endif;
Test1.cpp
# Include "stdafx. H "# include" test1.h "# include <iostream> # include" test2.h "// Note: void test1: classname (Test2 * Test2) {This-> Test2 = Test2; STD: cout <"test1";} void test1: refme () {STD :: cout <"refme test1" <STD: Endl ;}
I will not post tes2. In the same way, compile and run OK ....