使用cmake和g ++ / gcc的未定义引用

时间:2019-06-27 11:52:34

标签: c++ cmake

我正在努力理解为什么我的代码无法编译。目前,我的main.cpp文件看起来类似于

using namespace std;

#include <iostream>
#include <vector>

#ifdef __cplusplus
extern "C"{
#endif

#include "sparse.h"

#ifdef __cplusplus
}
#endif

int main(){

        std::vector<long> tmp;

        tmp.push_back(29);
        tmp.push_back(50);
        tmp.push_back(9);

        sparse<double> a(tmp);

        cout << a.getDimensions() << endl;
        cout << a.storageEfficiency() << endl;

        cout << a.get(15) << endl;

        a.set(18, 2.5);

        cout << a.storageEfficiency() << endl;
}

我的sparse.h文件如下所示:

using namespace std;

#include <iostream>
#include <numeric>
#include <vector>

template <class T>
class sparse
{
private:
        struct col {
                long index;
                T data;
                struct col* prev;
                struct col* next;
        };

        struct row {
                long index;
                struct col* cols;
                struct row* prev;
                struct row* next;
        };
        struct row* data;
        long dim;

public:
        sparse();
        sparse(vector<long> dimensions);
        T get(long i, long j);
        T get(long k);
        void set(long i, long j, T input);
        void set(long k, T input);
        long getDimensions();
        double storageEfficiency();
        ~sparse();
};

类似地,我的sparse.cpp看起来像这样:

using namespace std;

#ifdef __cplusplus
extern "C"{
#endif

#include "sparse.h"

#ifdef __cplusplus
}
#endif

template <class T>
sparse<T>::sparse(){
        data = new struct row;
        dim = 0;
}

template <class T>
sparse<T>::sparse(vector<long> dimensions){
        data = new struct row;

        dim = accumulate(dimensions.begin(), dimensions.end(), 0);
}

template <class T>
T sparse<T>::get(long i, long j){

        for (struct row* tmp_row = data; tmp_row != NULL; tmp_row = tmp_row->next){
                if (tmp_row->index > i)
                        return 0;
                else if (tmp_row->index == i){
                        for (struct col* tmp_col = tmp_row->cols; tmp_col != NULL; tmp_col = tmp_col->next){
                                if (tmp_col->index > j)
                                        return 0;
                                else if (tmp_col->index == j)
                                        return tmp_col->data;
                        }
                }

        }
        return NULL;
}

template <class T>
T sparse<T>::get(long k){
        return get(k%getDimensions(), k/getDimensions());
}

template <class T>
void sparse<T>::set(long i, long j, T input){

        if (i >= dim || j >= dim){
                // TODO: Throw C++ equivalent to ArrayOutOfBounds Exception
        }

        for (struct row* tmp_row = data; tmp_row != NULL; tmp_row = tmp_row->next){
                if (tmp_row->index > i){
                        struct row* rtmp = new struct row;
                        rtmp->index = i;
                        rtmp->next = tmp_row;
                        tmp_row = tmp_row->prev;
                        rtmp->next->prev = rtmp;
                        rtmp->prev = tmp_row;
                        tmp_row->next = rtmp;

                        struct col* ctmp = new struct col;
                        rtmp->cols = ctmp;
                        ctmp->index = j;
                        ctmp->data = input;
                        return;
                } else if (tmp_row->index == i){
                        for (struct col* tmp_col = tmp_row->cols; tmp_col != NULL; tmp_col = tmp_col->next){
                                if (tmp_col->index > j){
                                        struct col* ctmp = new struct col;
                                        ctmp->next = tmp_col;
                                        tmp_col = tmp_col->prev;
                                        ctmp->next->prev = ctmp;
                                        ctmp->prev = tmp_col;
                                        tmp_col->next = ctmp;

                                        ctmp->index = j;
                                        ctmp->data = input;
                                        return;
                                }
                                else if (tmp_col->index == j){
                                        tmp_col->data = input;
                                        return;
                                }
                        }
                }
        }
}

template <class T>
void sparse<T>::set(long k, T input){
        set(k%getDimensions(), k/getDimensions(), input);
}

template <class T>
long sparse<T>::getDimensions(){
        return dim;
}

template <class T>
double sparse<T>::storageEfficiency(){
        long numCells = 0;
        for (struct row* rtmp = data; rtmp != NULL; rtmp = rtmp->next){
                for (struct col* ctmp = rtmp->cols; ctmp != NULL; ctmp = ctmp->next){
                        numCells++;
                }
        }
        return (double)numCells/dim;
}

我的cmake文件如下:

cmake_minimum_required(VERSION 3.0.0)
project(SPARSE LANGUAGES CXX)

add_executable(
    sparse

    main.cpp
    sparse.cpp
)

无论如何,每当我使用cmake和gcc / g ++编译项目时,都会遇到类似以下错误:Template with C linkageundefined reference to 'sparse<double>::getDimensions()' 我该如何解决,为什么会发生?

0 个答案:

没有答案