我有3个类:A,B和C.C #include由B编辑,B #includ由A编辑.在C类中,我为按钮定义了一个处理程序,当按下按钮时,C将PostMessage对象A.我在C中包含A,我将有一个循环引用,那么我该怎么做才能避免这种循环引用?
编辑:所有包含都是在实现文件中.
尽管可视化工作室预编译器或其所谓的任何内容都将Graph识别为来自不同标头的类,但在构建之后我得到了最荒谬的错误,就好像我之前从未提及其他标题一样.首先我没有转发声明这两个类,下面的第一组错误来自于此,但后来我尝试了声明,并且类似的错误与类本身的结构有关.使用来自另一个类的函数生成它们,这些函数向我显示头文件没有任何内容.他们不了解彼此的功能,我不知道为什么.
Vertex.h:
#pragma once
#include "Graph.h"
#include <vector>
class Graph;
class Vertex
{
int unique_id;
int longestChain = 0;
int chainComponent_id;
std::vector<int> edges;
Graph* master;
public:
int get_id()
{
return unique_id;
}
int getChainComponent_id()
{
return chainComponent_id;
}
void setChainComponent_id(int id)
{
chainComponent_id = id;
}
int DFS(int, int);
Vertex(int id, std::vector<int> _edges, Graph* _master)
{
unique_id = id;
edges = _edges;
master = _master;
longestChain = 0;
chainComponent_id = -1;
}
};
Run Code Online (Sandbox Code Playgroud)
Graph.h:
#pragma once
#include "Vertex.h" …Run Code Online (Sandbox Code Playgroud) class Base {
public:
template<typename T>
static Base* construct() {
return new Derived<T>();
}
};
template<typename T>
class Derived : public Base {
public:
Derived() {}
};
Run Code Online (Sandbox Code Playgroud)
该代码生成编译错误(VS Studio 2017):
syntax error: unexpected token 'identifier', expected 'type specifier'
Run Code Online (Sandbox Code Playgroud)
这是无效的C ++吗?这种模式不可能吗?
i'm working with c++.
I need to create two classes that refering to each other.
Something like this:
class A {
private:
B b;
//etc...
};
class B {
private:
A a;
//etc...
};
Run Code Online (Sandbox Code Playgroud)
How can i do that?
Sorry for my bad English and thanks you for helping me :)