C++ 未定义的引用;函数名";在;“其他函数名”;C++;

C++ 未定义的引用;函数名";在;“其他函数名”;C++;,c++,makefile,undefined-reference,undefined-function,C++,Makefile,Undefined Reference,Undefined Function,作为项目的一部分,iam尝试从树cpp文件调用函数,以图形cpp 但是公司给了我一个错误,说我调用的函数没有定义 两天来,我一直试图解决这个问题,但都没有成功 我得到的错误是: bin/Graph.o: In function `Graph::BFS(Session&, int)': /home/spl211/CLionProjects/Assign1/src/Graph.cpp:24: undefined reference to `Tree::createTree(Session c

作为项目的一部分,iam尝试从树cpp文件调用函数,以图形cpp 但是公司给了我一个错误,说我调用的函数没有定义 两天来,我一直试图解决这个问题,但都没有成功

我得到的错误是:

bin/Graph.o: In function `Graph::BFS(Session&, int)':
/home/spl211/CLionProjects/Assign1/src/Graph.cpp:24: undefined reference to `Tree::createTree(Session const&, int)'
/home/spl211/CLionProjects/Assign1/src/Graph.cpp:39: undefined reference to `Tree::createTree(Session const&, int)'
这是我的图表cpp:谁实现了调用createTree(问题所在)的BFS算法

我明白了,你的方法是:

static Tree* createTree(const Session& session, int rootLabel){
在cpp文件中是静态的。因此,它在其他文件中不可见

您可能打算创建以下方法:

Tree* Tree::createTree(const Session& session, int rootLabel){

相同的主体,但签名应该是第二个主体。

您将
createTree
定义为:

static Tree* createTree(...)
这定义了一个具有内部链接的非成员函数,其他翻译单元看不到该函数

如果它应该是静态成员函数,则将其定义为成员函数(但不带
static
关键字):


为了发现问题,我们需要您的标题和makefile。可能是链接问题,也可能是未定义的方法。
createTree(…)
的定义不属于
类,因为您没有将
树::
放在前面。我认为这里不需要
静态
,它也没有你认为的含义。看来你的C++知识不足以用于这个练习。我建议您关注C++基础(定义/调用构造函数、声明方法、C++语法…)及其良好实践(缩进、行分割、……)。
static Tree* createTree(const Session& session, int rootLabel){
Tree* Tree::createTree(const Session& session, int rootLabel){
static Tree* createTree(...)
Tree* Tree::createTree(...)