重定义c ++ [英] redefinition c++

查看:255
本文介绍了重定义c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含在c ++中的头文件如何工作?我已经在.h文件中实现了类,并且在两个文件中存在 #include 时,出现此错误:

  files.h:14:7:错误:重新定义'class abstract_file'
files.h:14:20:error:'class abstract_file'`的前一个定义$对于每个类和枚举,b $ b

多次。
任何人都可以解释这个吗?

解决方案

一次只能定义一个定义,但头可以多次包含。要解决这个问题,请添加:

  #pragma once 

到每个头文件的顶部。



while #pragma once 是比较常见的,如果您使用的是较旧的编译器,则可能不支持。在这种情况下,你需要退回到手动包括卫兵:

  #ifndef MY_HEADER_H 
#define MY_HEADER_H

...

#endif

(注意你需要用每个头文件的唯一字符串替换 MY_HEADER_H

how does header including in c++ work? I have the classes already implemented in .h file and when there is #include in two files, there's this error:

files.h:14:7: error: redefinition of ‘class abstract_file’
files.h:14:20: error: previous definition of ‘class abstract_file’`

multiple times for each class and enum. Can anyone explain this?

解决方案

You can only include a definition one time but headers can be included multiple times. To fix that, add:

#pragma once

to the top of each header file.

While #pragma once is relatively common, if you are using an older compiler it may not be supported. In that case, you need to fall back on manual include guards:

#ifndef MY_HEADER_H
#define MY_HEADER_H

...

#endif

(note that you need to replace MY_HEADER_H with a unique string for each header file)

这篇关于重定义c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆