This is the problem that is encountered when making a module today. Abstract, the code is as follows:
File structure:
Error:
The error appears to be that the code in the Box.h is problematic. In fact, the root cause of the problem lies in the redundant Cup.h "Box.h" (because Box.h has # include "Cup.h", so it constitutes a loop resulting in an error! )
The solution, of course, is to delete the superfluous # include directive.
Feel C + + compiler basically error if you encounter more complicated situation, the error message basically has no reference value, in addition to misleading is misleading, so, in writing C + + must be careful and careful ah!
Code:
Box.h
#ifndef Box_h #define Box_h"Cup.h"class box{ public: Box (); ~Box ();}; Cup* getcup (); void Deletecup (Cup *p); #endif // Box_h
Box.cpp
#include"Box.h"#include<iostream>using namespacestd; Box::box () {cout<<"Box Cons"<<Endl;} Box::~Box () {cout<<"Box des"<<Endl;} Cup*Getcup () {return NewCup ();}voidDeletecup (Cup *p) {delete p;}
Cup.h
#ifndef Cup_h #define Cup_h"Box.h"class cup{ friend Cup* getcup (); Public : ~Cup (); Private : Cup ();}; #endif // Cup_h
Cup.cpp
" Cup.h " <iostream>usingnamespace std; Cup::cup () { "Cup cons" << Endl;} Cup::~Cup () { "Cup des" << Endl;}
Main.cpp
" Box.h " "Cup.h"int main () { *p = getcup (); box box; Deletecup (P);}
The consequences of the # include in C + +