【发布时间】:2014-06-29 10:41:33
【问题描述】:
我有两个 c++ 类,比如说:
class A{
//Stuff from class B
};
class B{
//Stuff from class A
};
我决定将接口与实现分开,所以我有 A.h 和 B.h 头文件。 如何管理包含? A.h 需要包含 B.h,反之亦然,但这给了我编译器错误。我应该如何进行?
编辑: 这些是标题。
顶点.h
/*
* Vertex.h
*
* Created on: Jun 24, 2014
* Author: marco
*/
#ifndef VERTEX_H_
#define VERTEX_H_
#include <string>
#include <iostream>
#include <map>
#include "Edge.h"
namespace MarcoGraphs {
class Vertex {
private:
//Variables
std::string id;
int soglia;
double peso;
bool visited,convinto;
std::map<std::string, Edge*> archi;
//Disabled operations
Vertex(const Vertex& param);
Vertex& operator=(const Vertex&);
Vertex(Vertex&&);
Vertex& operator=(Vertex&&);
public:
Vertex(std::string id);
~Vertex();
std::map<std::string, Edge*>::iterator begin();
std::map<std::string, Edge*>::iterator end();
bool operator==(const Vertex& param) const;
bool operator<(const Vertex& param) const;
std::string getId() const;
int getDegree() const;
void setSoglia(int soglia);
int getSoglia() const;
void setVisited(bool value);
bool isVisited() const;
void setConvinto(bool value);
bool isConvinto() const;
void setPeso(double peso);
double getPeso() const;
bool addEdge(Edge& param);
bool removeEdge(Edge& param);
};
std::ostream& operator<<(std::ostream& out, const Vertex& a);
} /* namespace MarcoGraphs */
#endif /* VERTEX_H_ */
Edge.h
/*
* Edge.h
*
* Created on: 29/giu/2014
* Author: Marco
*/
#ifndef EDGE_H_
#define EDGE_H_
#include "Vertex.h"
namespace MarcoGraphs {
class Edge {
private:
//Variables
Vertex* side1;
Vertex* side2;
//Forbidden operations
Edge(const Edge& e);
Edge& operator=(const Edge& e);
Edge(Edge&& e);
Edge&& operator=(Edge& e);
public:
Edge(const Vertex& v1, const Vertex& v2);
Vertex& getSide1();
Vertex& getSide2();
Vertex& getOtherEnd(const Vertex& thisEnd);
~Edge();
};
std::ostream& operator<<(std::ostream& out, const Edge& a);
} /* namespace MarcoGraphs */
#endif /* EDGE_H_ */
错误如下:
In file included from ..\Vertex.h:13:0,
from ..\Graph.h:10,
from ..\Graph.cpp:8:
..\Edge.h:17:2: error: 'Vertex' does not name a type
Vertex* side1;
^
..\Edge.h:18:2: error: 'Vertex' does not name a type
Vertex* side2;
^
..\Edge.h:25:13: error: 'Vertex' does not name a type
Edge(const Vertex& v1, const Vertex& v2);
^
..\Edge.h:25:21: error: ISO C++ forbids declaration of 'v1' with no type [-fpermissive]
Edge(const Vertex& v1, const Vertex& v2);
^
..\Edge.h:25:31: error: 'Vertex' does not name a type
Edge(const Vertex& v1, const Vertex& v2);
^
..\Edge.h:25:39: error: ISO C++ forbids declaration of 'v2' with no type [-fpermissive]
Edge(const Vertex& v1, const Vertex& v2);
^
..\Edge.h:26:2: error: 'Vertex' does not name a type
Vertex& getSide1();
^
..\Edge.h:27:2: error: 'Vertex' does not name a type
Vertex& getSide2();
^
..\Edge.h:28:2: error: 'Vertex' does not name a type
Vertex& getOtherEnd(const Vertex& thisEnd);
【问题讨论】:
-
通过在第二个类定义后加分号?
-
您可以在 B 类标头中转发声明 A 类并以这种方式使用它(和/或当然反之亦然)。但是,首先我会回去查看设计。首先可以简化/精简吗?
-
我认为在这种情况下我无法简化界面设计。我有一个使用 Edge 类的 Vertex 类,以及指向 Vertex 类的指针的 Edge 类(所以它需要 Vertex 类)
-
您应该继续向我们展示编译器错误。
标签: c++