The consequences of the # include in C + +

Source: Internet
Author: User

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 + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.