2015-09-02 85 views
0

我想写我的类和结构实现文件。该类有来自结构的变量。我不断收到错误:编译器错误:错误:'ListNode'没有命名一个类型

LinkedList.h:70:8:错误:'ListNode'没有命名一个类型 ListNode * head;

LinkedList.h:71:8:错误:'ListNode'没有指定类型 ListNode * cursor;

LinkedList.h看起来是这样的:

#ifndef CLASS_LINKEDLIST_H 
#define CLASS_LINKEDLIST_H 

// Header files 

#include <iostream> 
#include "ListNode.h" 

using namespace std; 

class LinkedList 
    { 
    public: 
    LinkedList(int maxSize = 10); 
    LinkedList(const LinkedList& other); 
    ~LinkedList(); 

    private: 

     ListNode* head; 
     ListNode* cursor; 

     int capacity; 
     int size; 
    }; 

#endif // ifndef CLASS_LINKEDLIST 

ListNode.h是这样的结构:

#ifndef STRUCT_LISTNODE_H 
#define STRUCT_LISTNODE_H 

using namespace std; 

struct ListNode 
    { 
    ListNode(int nodeData, ListNode* nextPtr); 
    int dataItem; 
    ListNode* next; 
    }; 
#endif // STRUCT_LISTNODE_H 

ListNode.cpp看起来是这样的:

#ifndef STRUCT_LISTNODE_H 
#define STRUCT_LISTNODE_H 

#include "ListNode.h" 
#include <iostream> 

ListNode:: ListNode(int nodeData, ListNode* nextPtr) 
    { 
    dataItem = nodeData; 
    next = nextPtr; 
    } 

#endif // STRUCT_LISTNODE_H 

这里是LinkedList.cpp的顶部

#ifndef STRUCT_LISTNODE_H 
#define STRUCT_LISTNODE_H 

// header files 
#include "LinkedList.h" 

我不允许更改.h文件,只是.cpp文件。我知道有一件简单的事情我忘记了,但我似乎无法弄清楚。

感谢您提前提供任何帮助。

+1

* LinkedList.cpp *为什么包含警卫?它是否被另一个文件包含? –

+4

是的,从你的CPP文件中删除包含警卫。包括警卫通常是说'#ifndef filename_h'的行。 –

+0

这是作业,你给了头文件?如果是这样,他们担心他们在头文件中使用名称空间标准符号......在头文件中使用名称空间是一个坏主意。 –

回答

0

包含警卫被放置,以便如果一个文件被包含多次,它只被使用一次。

当你包含一个文件,它是为所有意图和目的,复制粘贴到其中的文件,所以编译器的预处理器,ListNode.cpp看起来是这样的:

#ifndef STRUCT_LISTNODE_H 
#define STRUCT_LISTNODE_H 

#ifndef STRUCT_LISTNODE_H 
#define STRUCT_LISTNODE_H 

using namespace std; 

struct ListNode 
    { 
    ListNode(int nodeData, ListNode* nextPtr); 
    int dataItem; 
    ListNode* next; 
    }; 
#endif // STRUCT_LISTNODE_H 
#include <iostream> //contents of iostream go on for pages. Let's skip those, shall we? 

ListNode:: ListNode(int nodeData, ListNode* nextPtr) 
    { 
    dataItem = nodeData; 
    next = nextPtr; 
    } 

#endif // STRUCT_LISTNODE_H 

最新下来编译器看到:

#ifndef STRUCT_LISTNODE_H 

如果STRUCT_LISTNODE_H尚未确定,所做的一切,直到相应#endif

#define STRUCT_LISTNODE_H 

很酷。 STRUCT_LISTNODE_H刚刚定义。

#ifndef STRUCT_LISTNODE_H 

这次STRUCT_LISTNODE_H已经被定义,所以我们跳过了相应的ENDIF:

#endif // STRUCT_LISTNODE_H 

这是正确的,头文件几乎被完全抛弃。所以编译的是:

#ifndef STRUCT_LISTNODE_H 
#define STRUCT_LISTNODE_H 

#ifndef STRUCT_LISTNODE_H 
#endif // STRUCT_LISTNODE_H 
#include <iostream> //contents of iostream go on for pages. Let's skip those, shall we? 

ListNode:: ListNode(int nodeData, ListNode* nextPtr) 
    { 
    dataItem = nodeData; 
    next = nextPtr; 
    } 

#endif // STRUCT_LISTNODE_H 

有没有没有迹象ListNode的定义。

因此,这并不是说使用OP在cpp文件中包含警卫(并且不应该包含cpp文件,因此不需要包含警卫),但是OP使用相同的警卫包括两次警卫。

作为公共服务公告,关于using namespace std; 的常见警告那个邪恶的小捷径将整个标准库拉入全局命名空间。这意味着如果你有一个名为reverse的函数,它现在必须与std::reverse对抗,你可能不喜欢谁会赢,什么对你的程序执行起什么作用。

好吧。你的代码很好,但如果你包含别人的呢?现在他们与他们不打算的名字相冲突,并且你有他们从未见过的奇怪的错误信息,可能无法帮助你。

谨慎地使用它。性病无论是前缀::

std::cout << "HI!" << std::endl; 

或只拉你需要进入全局命名空间碎片

using std::cout; 
using std::endl; 

后来

cout << "HI!" << endl; 

更重要的是做这最后包括其他人的头部之后因为你不知道他们将如何回应。但这是你懒惰的问题。糟透了就是你。

而这使我们知道为什么你不把using namespace std;放在标题中。一些糟糕的吸盘包括你的标题来使用你的图书馆。开个唱!他们继承了你糟糕的编码风格。他们为你的错误而苦恼。这并不酷。

相关问题