我有一个名为File的类,它在标题" dmanager1.h"中定义(以及其他类)。在" dmanager1.cpp"文件(dmanager1.h文件的实现),当我按一个顺序列出标题时,我尝试与main.cpp一起编译时出错(main.cpp是空的,除了标题调用和空的&#34 ; int main()" ...基本上我只是测试类.h和.cpp文件)...如果我在dmanager1.cpp文件中切换标题我没有错误。我不明白发生了什么。我得到的错误是: 错误:'文件'没有命名类型
当我在我的" dmanager1.cpp"中订购了我的标题时,我得到了错误。如下:
#include "dmanager1.h"
#include <iostream>
#include <cstring>
如果我将标题切换为:
#include <iostream>
#include <cstring>
#include "dmanager1.h"
...我没有收到编译错误。第一个订单被解析有趣吗?任何想法都将不胜感激。
编辑:添加了有问题标题的一部分......
#ifndef _dmanager1_h
#define _dmanager1_h
//--------------------
// Forward References
//--------------------
// Node_L, Node_T, and Sector are defined in File: dmanager1a.h
class Node_L;
class Node_T;
class Sector;
class File
{
public:
// Default Constructor
//File();
// Constructor: Allowing "name", "size", and/or "permissions" to be set
// Permissions set to default of 0 == read and write
File(const char * & name, float size = 0, int permissions = 0) : timestamp(11223333) {};
// Default Destructor
~File();
//returns an int corresponding to the date modified (mmddyy)
int get_date_mod(void) const {return timestamp;}
// Return's current level of permission on the File: 0 = read/write, 1 = read only
int get_permission(void) const {return permission;}
// Set's Permission to "level": 0 = read/write, 1 = read only
int set_permission(int level);
private:
// Data members
char * name;
float size_OA;
//function used to update "date modified"
void update_timestamp(void);
// Current permission level of the file: 0 = read/write, 1 = read only
int permission;
//value modified by update_timestamp() and the value returned by get_date_mod(). Date file last edited.
int timestamp;
};
答案 0 :(得分:3)
您的dmanager1.h
标头很可能需要iostream
或cstring
定义的内容。
因此,它无法正确解析,编译器无法理解File
类的声明。
如果您发布dmanager1.h
文件,您将能够获得更详细的答案。
答案 1 :(得分:2)
确保每个标头完全自给自足。对于它使用的所有内容,它需要#include
标头,而不是假设它们将包含在其他内容中。即使它是.c文件包含的唯一标头,每个标头也应该有效。
我打赌您的dmanager1.h标头正在使用标准库中的某些内容而您没有包含它所需的标头。交换标题似乎可以解决问题,但这只是巧合。
您可以执行的一项诊断测试是创建一个只包含行#include "dmanager1.h"
的.c文件。尝试编译它。如果编译器抛出错误,它应该提供关于需要包含哪些附加头文件的提示。
更新:我可以使用您使用g++ -Wall
发布的标题的初始部分进行编译,我根本没有得到任何错误或警告。请发布一个重现问题的样本。