链接错误:未定义参照C++

问题描述:

我有不具有我不知道它是来自该未定义参考错误:链接错误:未定义参照C++

/usr/local/clion-2017.1.1/bin/cmake/bin/cmake --build /home/jscherman/CLionProjects/algo3-tp3-cmf/cmake-build-debug --target experimentos -- -j 4 
Scanning dependencies of target experimentos 
[ 50%] Building CXX object CMakeFiles/experimentos.dir/experimentos.cpp.o 
[100%] Linking CXX executable experimentos 
CMakeFiles/experimentos.dir/experimentos.cpp.o: In function `main': 
/home/jscherman/CLionProjects/algo3-tp3-cmf/experimentos.cpp:12: undefined reference to `cmfExacto(int, int, std::__cxx11::list<Eje, std::allocator<Eje> >&)' 
/home/jscherman/CLionProjects/algo3-tp3-cmf/experimentos.cpp:13: undefined reference to `heuristicaConstructiva(int, std::__cxx11::list<Eje, std::allocator<Eje> >)' 
collect2: error: ld returned 1 exit status 
CMakeFiles/experimentos.dir/build.make:94: recipe for target 'experimentos' failed 
make[3]: *** [experimentos] Error 1 
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/experimentos.dir/all' failed 
make[2]: *** [CMakeFiles/experimentos.dir/all] Error 2 
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/experimentos.dir/rule' failed 
make[1]: *** [CMakeFiles/experimentos.dir/rule] Error 2 
Makefile:118: recipe for target 'experimentos' failed 
make: *** [experimentos] Error 2 

experimentos.cpp(目标)

#include "cmf-algo-exacto.h" 
#include "cmf-heuristica-constructiva-golosa.h" 

int main(int argc, char** argv) { 
    int n = 5, m = 10; 
    std::list<Eje> grafo = Utils::generarGrafo(n, m, false, 0, 0); 
    std::cout << "Exacto: " << cmfExacto(n, m, grafo) << std::endl; 
    std::cout << "Constructiva: " << heuristicaConstructiva(n, grafo) << std::endl; 
    return 0; 
} 

CMF-heuristica-constructiva-golosa.h

​​

CMF-heuristica-constructiva.cpp

#include "cmf-heuristica-constructiva-golosa.h" 

Clique hconstructiva(int n, std::list<int> *listaAdyacencias){ 
    ... 
} 

Clique heuristicaConstructiva(int n, std::list<Eje> listaIncidencias) { 
    ... 
} 

int main(int argc, char** argv) { 
    ... 
    return 0; 
} 

CMF-ALGO-exacto.h

#ifndef TEST_DEBUGGER_CMF_ALGO_EXACTO_H 
#define TEST_DEBUGGER_CMF_ALGO_EXACTO_H 

#include <iostream> 
#include "Clique.h" 
#include "Eje.h" 
#include "DisjointSet.h" 
#include <list> 
#include "stringTokenizer.hpp" 
#include "Utils.h" 
#include <fstream> 

Clique * cmfExacto(DisjointSet &uds, std::list<Eje> ejesNoAgregados, list<int> *listaAdyacencias); 

Clique * cmfExacto(int n, int m, std::list<Eje> &listaIncidencias); 

#endif //TEST_DEBUGGER_CMF_ALGO_EXACTO_H 

CMF-ALGO-exacto.cpp

#include "cmf-algo-exacto.h" 

Clique * cmfExacto(int n, int m, std::list<Eje> &listaIncidencias) { 
    ... 
} 

Clique * cmfExacto(DisjointSet &uds, std::list<Eje> ejesNoAgregados, list<int> *listaAdyacencias){ 
    ... 
} 

int main(int argc, char** argv) { 
    ... 
    return 0; 
} 

因此,据我了解编译器大喊,它不是找到那些cmfExactoheuristicaConstructiva功能,但我看不出问题。这里有什么问题?

+0

分而治之。 –

+0

@RSahu说什么? – jscherman

+2

你已经声明并且正在调用一个函数'Clique heuristicaConstructiva(int n,std :: list &listaIncidencias);'它通过引用接受它的最后一个参数。但是你从来没有实现过这个功能。你已经实现了一个不同的,无关的函数'Clique heuristicaConstructiva(int n,std :: list listaIncidencias)',它的值是最后一个参数,但是你没有调用它。类似于'cmfExacto(int n,int m,std :: list &listaIncidencias)' –

你确定你真的在编译所有的cpp文件吗? 看起来像你的CMake /编译器不包括cmf-algo-exacto.cppcmf-heuristica-constructiva.cpp

此外,对于这些文件,由于您多次定义main函数,您可能会遇到错误。最好的做法可能是创建一个单独的main.cpp文件并将(仅)main函数放在那里。

编辑:伊戈尔Tandetnik是正确的,参数不匹配

+0

好的,谢谢。我很惭愧,事先没有看到这个问题。这个问题是因为我忘了编译这些.cpp文件。正如你所说,现在我现在遇到了那些主要功能的问题。 – jscherman