【发布时间】:2015-11-03 09:49:32
【问题描述】:
// q.h file
#ifndef __Q_H__
#define __Q_H__
using namespace std;
#include "n.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
class Q
{
public:
Q();
private:
N* beginning; //error N does not name a type
N* end; //error N does not name a type
int count;
};
#endif // end of file
// q.cpp file
#include "q.h"
#include "n.h"
#include <iostream>
using namespace std;
#include<string>
Q::Q()
{
beginning = NULL;
end = NULL;
}
// n.h file
#ifndef _N_H__
#define _N_H__
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "q.h"
class N
{
public:
N(int);
// next is a pointer object of type N
N* next;
// memeber for Node class
int value;
};
#endif
// n.cpp file
#include<string>
#include "q.h"
#include "n.h"
N::N(int v)
{
value = v;
}
我在以下几行中收到错误消息。我尝试更改用户命名空间标准;到每个文件的第一行,但这仍然不起作用。我也尝试过改变 q.h 和 n.h 的顺序,但它们仍然没有出现。
N* 开头; N* 结束;
我也知道一个很好的解决方案是使用“前向声明”,但这是我由某人提供给我的测试文件的一部分,因此我无法对类进行前向声明。
因此,如果有人可以请看一下,看看我如何解决这个问题。
谢谢。
【问题讨论】:
-
使用 g++ 编译没有任何错误。话虽如此,您有很多不必要的包含。你不需要
n.hinq.cpp,q.hinn.h和q.hinn.cpp -
除了循环包含之外,您还为标题保护使用保留标识符。它们不能以双下划线或下划线和大写字符开头。