gpt4 book ai didi

c++ - "undefined reference to"在 Ubuntu 上链接

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:32 25 4
gpt4 key购买 nike

我是 C 和 C++ 的新手,整整 2 天都在编译(或者我应该说链接)。任何人给我一个想法将不胜感激。错误消息和 3 个代码文件如下。这些是我从实际工作中削减到最少的内容,以便你们可以更好地了解。

环境:Ubuntu 10.10、Eclipse Indigo CDT、g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

错误信息:

**** Build of configuration Debug for project SceneRec2 ****

make all
Building file: ../src/AdaBoost.cpp
Invoking: GCC C++ Compiler
g++ -I"/home/ubuntuLove/Documents/workspace_eclipse/SceneRec2/Includes" -I/usr/src/linux-headers-2.6.35-30/arch/um/include/shared -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/AdaBoost.d" -MT"src/AdaBoost.d" -o "src/AdaBoost.o" "../src/AdaBoost.cpp"
Finished building: ../src/AdaBoost.cpp

Building file: ../src/AdaMain.cpp
Invoking: GCC C++ Compiler
g++ -I"/home/ubuntuLove/Documents/workspace_eclipse/SceneRec2/Includes" -I/usr/src/linux-headers-2.6.35-30/arch/um/include/shared -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/AdaMain.d" -MT"src/AdaMain.d" -o "src/AdaMain.o" "../src/AdaMain.cpp"
../src/AdaMain.cpp: In function ‘int main(int, char**)’:
../src/AdaMain.cpp:6: warning: deprecated conversion from string constant to ‘char*’
Finished building: ../src/AdaMain.cpp

Building target: SceneRec2
Invoking: GCC C++ Linker
g++ -o "SceneRec2" ./src/AdaBoost.o ./src/AdaMain.o
./src/AdaMain.o: In function `main':
/home/ubuntuLove/Documents/workspace_eclipse/SceneRec2/Debug/../src/AdaMain.cpp:5: undefined reference to `AdaBoost<double>::AdaBoost()'
/home/ubuntuLove/Documents/workspace_eclipse/SceneRec2/Debug/../src/AdaMain.cpp:6: undefined reference to `AdaBoost<double>::readFromFile(char*)'
/home/ubuntuLove/Documents/workspace_eclipse/SceneRec2/Debug/../src/AdaMain.cpp:8: undefined reference to `AdaBoost<double>::~AdaBoost()'
/home/ubuntuLove/Documents/workspace_eclipse/SceneRec2/Debug/../src/AdaMain.cpp:8: undefined reference to `AdaBoost<double>::~AdaBoost()'
collect2: ld returned 1 exit status
make: *** [SceneRec2] Error 1

**** Build Finished ****

注1。我在终端上执行 g++ 时收到相同的结果。

注2。链接器参数中.o 文件的路径应该是正确的(./src/###.o)。

AdaBoost.h

#ifndef _ADABOOST_H
#define _ADABOOST_H

#include <iostream>

const double eps = 2.2204e-16;

template<class T>
class AdaBoost
{
int N; //Number of Instances
int D; //Number of Dimensions
int nL; //Number of Learners / Classifiers / Rules

T** fVectors;
int* labels;

void learnRule(int t, double* dist);
double genRule(int t, int* L, double* dist);

public:

//Default Constructor
AdaBoost();

//Constructor
AdaBoost(T** data, int* labels, int n, int d, int nL);

//Train function
void train();

//Test function
void test(double** data, double* pMap);
void test(double** data, double* pMap, int n);

int writeToFile(char* fName);
int readFromFile(char* fName);

//Destructor
~AdaBoost();
};
#endif

AdaBoost.cpp

#include "AdaBoost.h"    
#include <fstream>

using namespace std;

template class AdaBoost<double> ;

template<class T>
int AdaBoost<T>::readFromFile(char* fName) {

ifstream inFile;

int temp;
int d, dir;
float thr, wt;

inFile.open(fName);
if (!inFile)
return 0;

inFile >> temp;
this->nL = temp;

int k = 0;
while (!inFile.eof() && k < nL) {

inFile >> d;
inFile >> thr;
inFile >> dir;
inFile >> wt;

k++;
}

inFile.close();

return 1;
}

AdaMain.cpp

#include "AdaBoost.h"
using namespace std;
int main(int argc, char** argv)
{
AdaBoost<double> rdClass;
rdClass.readFromFile("Naerer");

return 0;
}

最佳答案

如果使用显式实例化,则必须在实例化类之前定义成员函数的泛型版本:

template<class T>
int AdaBoost<T>::readFromFile(char* fName) {
// ...
}

template class AdaBoost<double>;

但是,如果您首先没有特定或紧迫的理由使用显式实例化,请遵循其他建议并在 header 中定义模板。

关于c++ - "undefined reference to"在 Ubuntu 上链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6672188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com