即使Visual Studio的预编译器或所谓的其他名称将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;
}
};
Graph.h:
#pragma once
#include "Vertex.h"
#include <vector>
#include <iostream>
class Vertex;
class Graph
{
std::vector<Vertex*> vertex;
int amountOfChainComponents = 0;
public:
Vertex* getVertex(int id)
{
if(id<0 || id>vertex.size())
{
return nullptr; //shouldn't be possible with proper input
}
return vertex[id];
}
int getAmountOfChainComponents()
{
return amountOfChainComponents;
}
int longestChain()
{
int longest = 0;
for(int i = 0; i < vertex.size(); i++)
{
if(vertex[i]->getChainComponent_id() == -1)
{
int tmp = vertex[i]->DFS(0, amountOfChainComponents);
amountOfChainComponents++;
if(tmp > longest)
{
longest = tmp;
}
}
}
if(longest == -1)
{
std::cout << "There is a chain for every positive integer" << std::endl;
return -1;
}
if(longest < 2)
{
std::cout << "There is no chain" << std::endl;
return 0;
}
return longest;
}
Graph(std::vector<std::vector<int>> vertices)
{
amountOfChainComponents = 0;
for(int i = 0; i < vertices.size(); i++)
{
Vertex* tmp = new Vertex(i, vertices[i], this);
vertex.push_back(tmp);
}
}
~Graph()
{
while(!vertex.empty())
{
delete vertex[vertex.size() - 1];
vertex.pop_back();
}
}
};
行严重性描述文件11错误语法错误:缺少';'
之前
'*'c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ vertex.h
34错误'_master':未声明
标识符c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ vertex.h
11错误,缺少类型说明符-假定为int。注意:C ++不会
支持
default-int c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ vertex.h
11前面的错误意外令牌
';' c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ vertex.h
30错误语法错误:标识符
'图形'c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ vertex.h
34错误'master':未声明
标识符c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ vertex.h
线路严重性描述文件
8错误'顶点':未声明
标识符c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ graph.h
向前声明后出现的错误:
行严重性描述文件28错误使用未定义类型
'顶点'c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ graph.h
28'-> getChainComponent_id'左边的错误必须指向
类/结构/联合/泛型
键入c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ graph.h
30错误使用未定义的类型
'顶点'c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ graph.h
30'-> DFS'左边的错误必须指向class / struct / union / generic
键入c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ graph.h
57错误使用未定义的类型
'顶点'c:\ users \ bico \ source \ repos \ longestchaingraph \ longestchaingraph \ graph.h
最佳答案
这是一个循环依赖问题。两个头文件相互包含。
对于这两种情况,仅forward declaration就足够了;声明指向类的指针并不需要该类为complete type。
顶点
#pragma once
#include <vector>
class Graph;
class Vertex
{
int unique_id;
int longestChain = 0;
int chainComponent_id;
std::vector<int> edges;
Graph* master;
};
图
#pragma once
#include <vector>
#include <iostream>
class Vertex;
class Graph
{
std::vector<Vertex*> vertex;
int amountOfChainComponents = 0;
};
编辑
将成员函数的实现移动到实现文件中。例如
顶点
#pragma once
#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();
...
};
顶点
#pragma once
#include "Vertex.h"
#include "Graph.h"
int Vertex::get_id()
{
return unique_id;
}
...
图
#pragma once
#include <vector>
#include <iostream>
class Vertex;
class Graph
{
std::vector<Vertex*> vertex;
int amountOfChainComponents = 0;
public:
Vertex* getVertex(int id);
...
};
Graph.cpp
#pragma once
#include "Vertex.h"
#include "Graph.h"
Vertex* Graph::getVertex(int id)
{
if(id<0 || id>vertex.size())
{
return nullptr; //shouldn't be possible with proper input
}
return vertex[id];
}
...
编辑2
正如@ M.M所指出的,对于
Graph
中的类Vertex.h
,正向声明就足够了。因此,您只需删除#include "Graph.h"
中的Vertex.h
,并在#include "Vertex.h"
中保留Graph.h
。关于c++ - 不同标题中的类不能互相识别吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52531872/