Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single class has a Class Redefinition Error

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:

#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.

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.

like image 737
Mister R2 Avatar asked Jan 22 '12 19:01

Mister R2


People also ask

What to put in header c++?

Usually you should be using fully qualified names in a header file, such as “std::ostream.” file for the module. Put structure and class declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

Which two files are usually defined for a class?

C++ classes (and often function prototypes) are normally split up into two files. The header file has the extension of . h and contains class definitions and functions. The implementation of the class goes into the .


3 Answers

You need header guards on 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.

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

#pragma once

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

like image 71
Drew Dormann Avatar answered Oct 18 '22 13:10

Drew Dormann


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

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

In short, there are two ways to do it

Version 1, #defined include guards

#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.

Version 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.

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

like image 45
Joachim Isaksson Avatar answered Oct 18 '22 13:10

Joachim Isaksson


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.

like image 44
Martin Brandl Avatar answered Oct 18 '22 13:10

Martin Brandl