and*_*rew 2 c++ class redefinition
我正在尝试编译我的代码g++,但它抛出这个编译错误:
Enrollment.h:3:7: error: redefinition of class sict::Enrollment
Enrollment.h:3:7: error: previous definition of class sict::Enrollment
Run Code Online (Sandbox Code Playgroud)
我的Enrollment.h:
namespace sict{
class Enrollment{
private:
char _name[31];
char _code[11];
int _year;
int _semester;
int _slot;
bool _enrolled;
public:
Enrollment(const char* name , const char* code, int year, int semester , int time );
Enrollment();
void set(const char* , const char* , int ,int, int , bool = false );
void display(bool nameOnly = false)const;
bool valid()const;
void setEmpty();
bool isEnrolled() const;
bool hasConflict(const Enrollment &other) const;
};
}
Run Code Online (Sandbox Code Playgroud)
有任何解决这个问题的方法吗?
问题可能是您的头文件(直接和间接)包含在同一个翻译单元中.您应该使用某种方法来避免cpp中相同头文件的多个包含.我更喜欢#pragma once你的头文件的开头 - 它不是标准的,但它得到了所有主要编译器的支持.否则你可以去找好老包括警卫:
#ifndef _Enrollment_h_
#define _Enrollment_h_
// Your header contents here
#endif
Run Code Online (Sandbox Code Playgroud)
或者用pragma:
#pragma once
// Your header contents here
Run Code Online (Sandbox Code Playgroud)