2

I have a very simple project. The file main.cpp creates a vector of floats, sorts them, then prints them.

I compile it with:

g++ main.cpp quickSort.cpp

If in main.cpp I change

#include "quickSort.h"

to

#include "quickSort.cpp"

it works fine.

How do I change this project so that it works with the header file?

main.cpp

#include <iostream>
#include <random>
#include <vector>
#include "quickSort.h"

#define MODULO 200
#define SIZE 1500

int main(){
    std::vector<float> data;
    for(int i=0;i<SIZE;i++)
        data.push_back(rand()%MODULO);

    data=quickSort(data);

    for(int i=0;i<data.size();i++){
        std::cout<<data[i]<<',';
    }
    std::cout<<'\n';
    return 0;
}

quickSort.cpp

#include <vector>

template<typename T>

std::vector<T> quickSort(std::vector<T> data){

    if(data.size()==0)return data;
    int pivot=data[data.size()/2];

    std::vector<T> a;
    std::vector<T> b;
    std::vector<T> c;
    std::vector<T> ret;

    for(int i=0;i<data.size();i++){
        if(data[i]<pivot)a.push_back(data[i]);
        if(data[i]==pivot)b.push_back(data[i]);
        if(data[i]>pivot)c.push_back(data[i]);
    }

    a=quickSort(a);
    c=quickSort(c);

    for(int i=0;i<a.size();i++)ret.push_back(a[i]);
    for(int i=0;i<b.size();i++)ret.push_back(b[i]);
    for(int i=0;i<c.size();i++)ret.push_back(c[i]);

    return ret;
}

quickSort.h

#include <vector>
template<typename T>

std::vector<T> quickSort(std::vector<T> data);
Display name
  • 721
  • 2
  • 11
  • 29
  • Do you mean that quickSort.cpp is not being compiled and included in the project? Depending on the platform there will be a way to add multiple files into the project. For example, g++ main.cpp quickSort.cpp -I . -o qs will do it on Linux. Visual Studio has a simple way to add multiple cpp files to the project. – Matthew Fisher Sep 26 '18 at 01:02
  • @MatthewFisher I'm on Linux, what does the "-I . -o qs" do? – Display name Sep 26 '18 at 01:06
  • @KenY-N has a good point. Your quickSort function is a template so it should be in the header file anyway. – Matthew Fisher Sep 26 '18 at 01:07
  • The -I flag for g++ tells the complete to look in a particular directory. The '.' means look in the current directory. The -o flag names the output binary, in this case 'qs' – Matthew Fisher Sep 26 '18 at 01:08
  • @MatthewFisher OK, so I should copy quickSort.cpp to quickSort.h? – Display name Sep 26 '18 at 01:08
  • yes and drop quickSort.cpp altogether – Matthew Fisher Sep 26 '18 at 01:08
  • @MatthewFisher ok so why doesn't 'g++ main.cpp quickSort.cpp -I . -o qs' compile the project? – Display name Sep 26 '18 at 01:11
  • Have a look at the link @KenY-N posted. template belong in header files since they are need at compile in other cpp files – Matthew Fisher Sep 26 '18 at 01:12
  • @MatthewFisher I can't see the link you're referencing.... – Display name Sep 26 '18 at 01:17
  • @MatthewFisher I think he deleted the link, but the message that my question is a duplicate probably refers to the same question. Even with that, I still don't know how to change my program so that it works with the header file... – Display name Sep 26 '18 at 01:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180767/discussion-between-matthew-fisher-and-sam). – Matthew Fisher Sep 26 '18 at 01:23

0 Answers0