C + + header file Repeat contains solution

Source: Internet
Author: User

This type of error is often seen in C/+ + programming:error:redefinition of ' struct student ' or error:previous definition of ' struct Student '.

Literally, it refers to a redefinition or previously defined. Below I will give a set of system solutions for this type of problem, after reading the following article, I believe that after this problem will be confident, the design of the program is more reasonable.

To get straight to the point, I'll give you a concrete example.

1. Examplestruct.h
struct student{    char name[20];    int age;};

Club.h
#include "student.h" #include <vector>struct club{    char name[20];    Std::vector<student> members;};

Main.cpp
#include <iostream> #include <cstring> #include "student.h" #include "club.h" using namespace Std;int main () {    club Club1;    Student stu1{"Jin";    Student stu2{"Shia", n};    strcpy (Club1.name, "Gamelover");    Club1.members.push_back (STU1);    Club1.members.push_back (STU2);    for (auto stu:club1.members)    {        cout<<stu.name<<endl;    }}

This program will output the above error. First we understand that the C + + header file is equivalent to inserting the included file in the compiler. Therefore, for the final main.cpp, contains two times student.h, solves this problem method to have the following several.1.1 using Ifndef to avoid duplicate header file inclusionswe recall the basic syntax we learned: #ifndef N and # define N. The former is if n is not defined, executes the following statement, which is the definition N. If you do not remember please turn over the book or Google. Plus after the code is also given out. student.h
#ifndef student_h#define student_hstruct student{    Char name[20];    int age;}; #endif//Student_h

Club.h
#ifndef club_h#define club_h#include "student.h" #include <vector>struct club{    char name[20];    Std::vector<student> members;}; #endif//Club_h

Main.cpp
#include <iostream> #include <cstring> #include "student.h" #include "club.h" using namespace Std;int main () {    club Club1;    Student stu1{"Jin";    Student stu2{"Shia", n};    strcpy (Club1.name, "Gamelover");    Club1.members.push_back (STU1);    Club1.members.push_back (STU2);    for (auto stu:club1.members)    {        cout<<stu.name<<endl;    }}

For a method, we should try to understand its essence, why add these statements have effect? In fact, at compile time, each file can only be compiled once (another way to understand the final file can not have a naming conflict), and each header file is compiled, will define a xxx_h variable to identify, to avoid repeated compilation method is to determine whether the xxx_h can be defined. Since this is simple, the compiler actually has a simpler solution.1.2 Compiler commandsThis approach is not supported at the C + + language level, but as more and more compilers support this scenario, it is actually a fact that  #pragma once. Is it over here? No! In some cases this method also fails, such as ...
2. Including each othermake a little change to the student.h above.
#pragma once#include "club.h" struct student{    char name[20];    int age;    Club MyClub;};

A new error has been found! This is because of references to each other, such as the club file in student, the club also refers to the student, and the syudent file, so loop until the stack overflows. In this case, both of the above methods are useless. Here we have to think about this: Since the header file is simply a code insertion, we simply declare the desired object in a single file. Such as:
#pragma onceclass club;struct student{    Char name[20];    int age;    Club MyClub;};
But we found it was still wrong!! This error is: Error:field ' myclub ' has incomplete type. In fact, because the club is not defined here, it does not know its size, myclub size is unknown. We changed a little bit:
#pragma onceclass club;struct student{    Char name[20];    int age;    Club *myclub;};
That's all you can do.

3, summed up C + + is a relatively complex language, its grammatical complexity, all-encompassing characteristics, resulting in countless traps, mastering it requires a larger effort, as the novice, have to continue efforts.

C + + header file Repeat contains solution

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.