0

I'm doing a little research for a course on which computing speed over collections on different cpu types. I'm writing my tests in C++ (bad choice as I started with the language three weeks ago) and I'm trying to measure the amount of clock ticks.

I thought I'd implement an arbitrary map (yes, yes, transform) function first. For some reason I decided to encapsulate the functions I'm writing in different classes. I first tried making a function that takes an arbitrary collection, but that didn't work, so I thought of making a function that takes two iterator pointers, so I'm currently having problems with this:

.h

#include <vector>
#include <map>
#include <ctime>
using namespace std;

class MapTests {
public:
    static unsigned long halve(unsigned long in);

    template <class InputIter>
    static clock_t getHalveTest(InputIter begin, InputIter end);
};

.cpp

#include "MapTests.h"

unsigned long MapTests::halve(unsigned long in){ return in/2; }

template<class InputIter>
clock_t MapTests::getHalveTest(InputIter begin, InputIter end) {
    vector<unsigned long> store = {};
    auto beginTime = clock();
    transform(begin, end, store, &MapTests::halve);
    return clock()-beginTime;
}

I'm still planning on making a seperate function for std::map, but this is what I was planning to use for list, vector, array and such.

When I try to compile and print the clock ticks for the forward_list I get this monster of an error:

Undefined symbols for architecture x86_64: "unsigned long MapTests::getHalveTest*> >(std::__1::__forward_list_iterator*>, std::__1::__forward_list_iterator*>)", referenced from: _main in main.cpp.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

And I have no clue what I'm doing wrong here. I suspect I should do something with my template, but I don't know what.

Typhaon
  • 828
  • 8
  • 27
  • Your code is doing the moral equivalent of MapTests.h `#define GET_HALVE_TEST /*macro body in MapTests.cpp*/` and main.cpp that uses the macro doesn't see anything. – Eljay Oct 10 '19 at 16:28
  • Sorry, but I don't know what you mean by this, can you elaborate please? – Typhaon Oct 10 '19 at 17:04
  • Then my analogy was poor. A template is instantiated on demand. Your `main.cpp` tries to instantiate the template, but cannot, because its implementation was in a different file. – Eljay Oct 10 '19 at 17:21
  • Oh wow I see what you're saying. I got it working now. Thanks a lot! – Typhaon Oct 10 '19 at 19:06

0 Answers0