包含在 C++ 中的 header 如何工作?我已经在 .h 文件中实现了类,当两个文件中有 #include
时,出现此错误:
files.h:14:7: error: redefinition of ‘class abstract_file’
files.h:14:20: error: previous definition of ‘class abstract_file’`
每个类和枚举多次。 谁能解释一下?
最佳答案
在 C++ 中使用 include
只需获取包含的文件并将内容展开到包含它的位置。要做到这一点而不用担心同一文件的多个包含,您需要使用 header 保护。对所有头文件使用这种基本格式:
#ifndef FILENAME_H
#define FILENAME_H
class foo (or whatever else is in the file!) {
...
};
#endif
关于c++ - 重新定义 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2823330/