0

クラスプロジェクトに CMake (CLion) を使用しています。次の解決策を試しましたが、うまくいきませんでした: 123

クラス プロジェクト用に HeapSort クラスを作成しましたが、それを使用して文字列のベクトル (辞書) を並べ替える必要があります。このディクショナリに HeapSort クラスのインスタンスを次のように作成します。

void Dictionary::heapSort()
{
    Heap<std::string> h;
    stringDict = h.heapSort(stringDict);
}

heapSort()クラスが次のように定義されているコンストラクターと関数への未定義の参照を取得し続けます(heapSort()同様です):

ヒープ.cpp

#include "Heap.h"
template <typename T>
Heap<T>::Heap() {}

template <typename T>
void Heap<T>::initializeMaxHeap(std::vector<T> v)
{
    heap = v;
    heapSize = heap.size();
}

template <typename T>
void Heap<T>::maxHeapify(int i)
{
    int l = left(i);
    int r = right(i);
    int large;
    if (l <= heapSize && heap.at(l) > heap.at(i))
        large = l;
    else
        large = i;
    if (r <= heapSize && heap.at(r) > heap.at(i))
        large = r;
    if (large != i)
    {
        std::swap(heap.at(i), heap.at(large));
        maxHeapify(large);
    }
}

template <typename T>
void Heap<T>::buildMaxHeap()
{
    for (int i = std::floor(heap.size()/2); i > 1; i++)
        maxHeapify(i);
}

template <typename T>
std::vector<T> Heap<T>::heapSort(std::vector<T> v)
{
    initializeMaxHeap(v);
    buildMaxHeap();
    for (int i = heap.size(); i > 2; i++)
    {
        std::swap(heap.at(1), heap.at(i));
        heapSize--;
        maxHeapify(1);
    }
    return heap;
}

Heap.h

#ifndef PROJ3_HEAP_H
#define PROJ3_HEAP_H

#include <cmath>
#include <vector>

#include "Dictionary.h"

template <typename T>
class Heap
{
private:
    std::vector<T> heap;
    int heapSize;

public:
    Heap();
    int parent(int index) { return index/2; };
    int left(int index) { return index * 2; };
    int right(int index) { return index * 2 + 1; };
    int getItem(int index) { return heap.at(index); };
    void initializeMaxHeap(std::vector<T> v);
    void maxHeapify(int i);
    void buildMaxHeap();
    std::vector<T> heapSort(std::vector<T> v);

};


#endif //PROJ3_HEAP_H

CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
project(proj3)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp Dictionary.cpp Dictionary.h Grid.cpp Grid.h Heap.cpp Heap.h)
add_executable(proj3 ${SOURCE_FILES})

現在、すべてのファイルを 1 つのフォルダーにまとめています。CMakeLists.txtこの時点で何を追加する必要がありますか? 私はadd_library/target_link_library(異なる順序で)試してinclude_directoriesみました. ディレクトリも再編成する必要がありますか?

編集: Heap.cpp と Heap.h を追加

4

0 に答える 0