C++ 类找不到内部定义的C++;类内部模板

C++ 类找不到内部定义的C++;类内部模板,c++,compiler-errors,C++,Compiler Errors,我有下面的代码,编译器错误是: 为什么graph_算法不能在graph类中找到顶点类 graph_algorithms.h:26:33: error: ‘Graph::graph<T>::vertex’ is not a type graph_algorithms.h:26:55: error: ‘Graph::graph<T>::vertex’ is not a type graph_algorithms.h: In member function ‘bool Grap

我有下面的代码,编译器错误是: 为什么graph_算法不能在graph类中找到顶点类

graph_algorithms.h:26:33: error: ‘Graph::graph<T>::vertex’ is not a type
graph_algorithms.h:26:55: error: ‘Graph::graph<T>::vertex’ is not a type
graph_algorithms.h: In member function ‘bool Graph::graph_algorithm<T>::vertex_comparer::operator()(int&, int&)’:
graph_algorithms.h:28:10: error: ‘t1’ was not declared in this scope
graph_algorithms.h:28:21: error: ‘t2’ was not declared in this scope
graph_algorithms.h:28:33: error: expected primary-expression before ‘return’
graph_algorithms.h:28:33: error: expected ‘:’ before ‘return’
graph_algorithms.h:28:33: error: expected primary-expression before ‘return’
graph_algorithms.h:28:33: error: expected ‘;’ before ‘return’
graph_algorithms.h:29:7: warning: no return statement in function returning non-void [-Wreturn-type]
graph_算法。h:26:33:错误:“graph::graph::vertex”不是类型
graph_algorithms.h:26:55:错误:“graph::graph::vertex”不是类型
graph_algorithms.h:在成员函数“bool graph::graph_algorithm::vertex_comparer::operator()(int&,int&)”中:
graph_algorithms.h:28:10:错误:“t1”未在此范围内声明
graph_algorithms.h:28:21:错误:“t2”未在此范围内声明
graph_algorithms.h:28:33:错误:“return”之前应为主表达式
graph_algorithms.h:28:33:错误:在“return”之前应为“:”
graph_algorithms.h:28:33:错误:“return”之前应为主表达式
graph_algorithms.h:28:33:错误:应为“;”在“返回”之前
graph_algorithms.h:29:7:警告:返回non void[-Wreturn type]的函数中没有返回语句

#ifndef图_
#定义图_
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间图
{
模板
类图
{
//转发声明
私人:
类顶点;
类边缘;
模板
友元类图算法;
公众:
显式图(常数std::向量和顶点);
~graph()
{}
通过键(T键1、T键2)无效插入顶点对;
常量std::list&vertices()常量{返回m_顶点;}
//私有包含类
私人:
结构边
{
边(顶点*边,T权重):
m_边(边),
单位重量(重量)
{}
顶点*m_边;
T mu重量;
};//端边
类顶点
{
公众:
顶点(T键):
m_键(键)
{}
空心连接_边(顶点*相邻);
const T key()const{return m_key;}
常量std::list&edges()常量{return m_edges;}
私人:
std::列出m_边;
T m_键;
布尔包含边到顶点,带关键点(常量T关键点);
};//结束顶点
//私有方法和成员变量
私人:
std::列出m_顶点;
顶点*包含_顶点(常数键);
};
}
/*!
*图的构造函数:以一对顶点作为连接,尝试
*如果图形中尚未插入,则插入。然后在边列表中连接它们
*/
模板
图::图::图(常数std::向量和顶点关系)
{
#ifndef NDEBUG
std::cout key()==key){
返回true;
}   
}
返回false;
}
#恩迪夫

\ifndef图算法_
#定义图算法_
#包括“graph.h”
名称空间图
{ 
模板
类图算法
{
//提前声明
//内部类
私人:
类顶点比较器;
公众:
图\算法(图*&图):
m_图(图)
{}
//内部类的定义
私人:
类顶点比较器
{
公众:
布尔运算符()(图::顶点&v1,图::顶点&v2)
{
(t1.key()

#包括“graph_algorithms.h”
#包括“graph.h”
#包括
int main(int argc,char*argv[])
{
std::向量图;
对于(int i=0;i<100;i++){
图形向量推回(std::pair(rand()%20,rand()%20));
}
Graph::Graph my\u Graph(Graph\u vect);
返回0;
}

通常,如果在使用模板时看到编译错误,那么前几个疑问之一应该是您忘记为依赖类型编写
typename
,例如:

bool operator()(graph<T>::vertex &v1, graph<T>::vertex &v2)
bool操作符()(图::顶点&v1,图::顶点&v2)
应该是这样的:

bool operator()(typename graph<T>::vertex &v1, typename graph<T>::vertex &v2)
bool操作符()
您是否看到
typename
?同样,您也需要在代码中的任何地方编写
typename
,无论您在哪里使用依赖类型

如果您想知道什么是依赖类型,以及将
typename
与依赖类型一起放置在何处以及为什么放置,请阅读以下内容:


除此之外,您还需要了解
private
成员和
public
成员之间的区别——无论是类型还是变量。在您的例子中,嵌套类型是
private
,我认为应该是
public

您的vertex类是private。我认为类edge的正向声明是错误的:您改为编写结构edge。@chac感谢您的更正。它没有被使用,所以编译器从未使用过complained@OrgnlDave是的,但图形算法类是一个朋友。我不希望我的库的用户不得不担心顶点或边的概念,所以我在内部将它们设置为私有的。但是引用类被声明为friend。还不够吗?@chac:我没注意到。另外,我也不确定语法。@Nawaz谢谢你的帮助,关于typename你是对的。是的,我将顶点和边设置为私有,因为我不想让用户担心这个概念。如果他们需要来自顶点或边的数据,我将提供一个T&get_data()方法。但我仍然希望图形算法能够访问这些,因为它需要它。所以我把它当成了朋友。
#include "graph_algorithms.h"
#include "graph.h"
#include <cstdlib>

int main(int argc, char *argv[])
{
  std::vector<std::pair<int, int> > graph_vect;
  for (int i = 0; i < 100; i++) {
    graph_vect.push_back(std::pair<int, int>(rand()%20, rand()%20));
  }
  Graph::graph<int> my_graph(graph_vect);
  return 0;
}
bool operator()(graph<T>::vertex &v1, graph<T>::vertex &v2)
bool operator()(typename graph<T>::vertex &v1, typename graph<T>::vertex &v2)