C++ 单个类有类重定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8964164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:23:55  来源:igfitidea点击:

Single class has a Class Redefinition Error

c++classredefinition

提问by Mister R2

I'm new to C++, and I'm having a problem with my class definitions in a header file. The code for the header file (Student.h) is:

我是 C++ 新手,我的头文件中的类定义有问题。头文件(Student.h)的代码是:

#include <string>
using namespace std;

class Student
{
  // Data Members for a Student
  string id;
  string preferences[3];
  int skill;

  // Constructor
 public:
  Student(){}

 public:
  void SetID(string str)
  { this->id = str; }
 public:
  void SetSkill(int i)
  { this->skill = i; }
 public:
  void SetPreferences(int i, string s)
  {
    this->preferences[i] = s;
  }
};

class StudentSchedule
{
 public:
  StudentSchedule(){}
};

The compiler error says that line 14 (class Student) is a redefinition of 'Student', and that line 15 ({ -- the open brace following class Student) is the previous definition of 'Student'. The same error on the first two consecutive lines exists for the StudentSchedule class.

编译器错误说第 14 行(Student 类)是“Student”的重新定义,第 15 行({ - Student 类后面的左大括号)是“Student”的先前定义。StudentSchedule 类的前两行也存在相同的错误。

I have no .c, .cpp, or .h files anywhere in my compilation that define either class. I have no idea why I'm getting this error.

我的编译中的任何地方都没有定义任一类的 .c、.cpp 或 .h 文件。我不知道为什么会出现此错误。

回答by Drew Dormann

You need header guardson that header file. It is presumably being included twice.

您需要对该头文件进行头保护。它大概被包括两次。

Modify the header, adding these lines to the beginning and end.

修改标题,将这些行添加到开头和结尾。

#ifndef STUDENT_H
#define STUDENT_H

// Put the entire contents of your header here...

#endif

The define doesn't need to be STUDENT_H... it just needs to be unique.

定义不需要是STUDENT_H......它只需要是唯一的。

With these directives added, the compiler will ignore all contents of the header file if it has already been parsed.

添加这些指令后,如果头文件已经被解析,编译器将忽略头文件的所有内容。

Alternatively, while it is not standard C++, all major compilers will allow you to put a single

或者,虽然它不是标准的 C++,但所有主要编译器都允许您放置一个

#pragma once

as the first line of the header to prevent it from being parsed multiple times.

作为标题的第一行,以防止它被多次解析。

回答by Joachim Isaksson

You're probably including the .h file twice, the first time it will define Student, the second it will try to redefine it.

您可能两次包含 .h 文件,第一次它会定义 Student,第二次它会尝试重新定义它。

See the Wikipedia entry on include guardsfor a more extensive explanation of the problem and information on how to avoid it.

有关该问题的更广泛解释以及有关如何避免该问题的信息,请参阅Wikipedia 上关于 include 守卫的条目

In short, there are two ways to do it

简而言之,有两种方法可以做到

Version 1, #defined include guards

版本 1,#defined 包含守卫

#ifndef STUDENT_HPP
#define STUDENT_HPP

...your code here...

#endif

Usually the #define is called some variation of the file name since it has to be different in every include file.

通常#define 被称为文件名的一些变体,因为它在每个包含文件中都必须不同。

Version 2, #pragma once

版本 2,#pragma once

#pragma once

...your code here...

This pragma is (as most pragmas) not portable to all compilers, but some of the most important ones. It also has the advantage of not needing a manually assigned name.

这个 pragma(和大多数 pragma 一样)不是可移植到所有编译器,而是一些最重要的编译器。它还具有不需要手动分配名称的优点。

Which you use is up to you, but you most likely have to pick one :)

您使用哪个取决于您,但您很可能必须选择一个:)

回答by Martin Brandl

I prefer using

我更喜欢使用

   #pragma once

as the first line of a header file, instead of the defines. Even if this is non-standard, it does avoid name clashes and can reduce compile time.

作为头文件的第一行,而不是定义。即使这是非标准的,它也可以避免名称冲突并可以减少编译时间。

回答by DotNetUser

When I learnt c++ our professor said, the first two lines that you write in c++ class should always be #ifndef followed by #define. This prevents multiple definitions in header files

当我学习 c++ 时,我们的教授说,你在 c++ 类中编写的前两行应该始终是 #ifndef,然后是 #define。这可以防止头文件中的多个定义

http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html

http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html