'[Class name]'没有命名C ++中的类型 [英] '[Class name]' does not name a type in C++
问题描述
我使用列表列表对图形进行编程。为此,我有两个类,每个类都有一个指向同一个类的另一个对象的指针和一个指向第二个类的对象的指针。以下是代码:
I am programming a graph using a list of lists. For that, I have two classes, and each one of this classes has a pointer to another object of the same class and a pointer to the object of the second class. Here is the code:
文件Vh:
#ifndef VERTICEPUNT_H
#define VERTICEPUNT_H
#include "A.cpp"
typedef char E;
class V
{
public:
E etiqueta;
V* siguiente;
A* primera; //<- Error: 'A' does not name a type
V();
~V();
};
#endif // VERTICEPUNT_H
#include "V.h"
V::V()
{
etiqueta = ' ';
siguiente = 0;
primera = 0; //<- Error: 'primera' was not declared in this scope
}
V::~V()
{
delete primera;
delete siguiente;
}
文件啊:
$ b
File A.h:
#ifndef ARISTAPUNT_H
#define ARISTAPUNT_H
#include "V.cpp"
typedef int P;
class A
{
public:
P peso;
V* vertice;
A* siguiente;
A();
~A();
};
#endif // ARISTAPUNT_H
/ em>
A.cpp:
#include "A.h"
A::A() //<- Error: 'A' does not name a type
{
peso = 0;
siguiente = 0;
vertice = 0;
}
A::~A() // <- Error: 'A' does not name a type
{
delete siguiente;
}
如何解决这个问题?
推荐答案
该消息表示类名不在范围内。 Vh
不应包括 A.cpp
,应包括 Ah
。 包含
V.cpp
也同样适用 Ah
。
The message means that the class name is not in scope. V.h
should not include A.cpp
, it should include A.h
. Same goes for A.h
inclusion of V.cpp
.
一般来说,您不需要 #include
一个CPP文件 - 来自标题或来自另一个CPP文件。只有 .h
头文件被设计为由预处理器包含。
In general, you never want to #include
a CPP file - from a header or from another CPP file. Only .h
header files are designed for inclusion by preprocessor.
向前声明类,你正在定义的指针,并放弃包含该类的头:
In case of circular definitions like this, you should forward-declare the class the pointer to which you are defining, and forego inclusion of that class's header:
#ifndef VERTICEPUNT_H
#define VERTICEPUNT_H
class A; // <<== Forward declare the class.
typedef char E;
class V
{
public:
E etiqueta;
V* siguiente;
A* primera;
V();
~V();
};
#endif // VERTICEPUNT_H
这篇关于'[Class name]'没有命名C ++中的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!