2017-06-20 104 views
0

我有不具有我不知道它是來自該未定義參考錯誤:鏈接錯誤:未定義參照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)' –

回答

2

你確定你真的在編譯所有的cpp文件嗎? 看起來像你的CMake /編譯器不包括cmf-algo-exacto.cppcmf-heuristica-constructiva.cpp

此外,對於這些文件,由於您多次定義main函數,您可能會遇到錯誤。最好的做法可能是創建一個單獨的main.cpp文件並將(僅)main函數放在那裏。

編輯:伊戈爾Tandetnik是正確的,參數不匹配

+0

好的,謝謝。我很慚愧,事先沒有看到這個問題。這個問題是因爲我忘了編譯這些.cpp文件。正如你所說,現在我現在遇到了那些主要功能的問題。 – jscherman