0

datagenerator.hpp

#ifndef STOCK_INDICATOR_SOURCE_PROJECT_DATAGENERATOR_H
#define STOCK_INDICATOR_SOURCE_PROJECT_DATAGENERATOR_H

#include <random>

class RandomNumberRange{
public:
    RandomNumberRange(int low, int high);
    double operator()();
private:
    std::mt19937 random_engine_;
    std::uniform_int_distribution<> distribution_;
};

void generate(std::vector<int> data, RandomNumberRange generator);

#endif

datagenerator.cpp

class RandomNumberRange
{
public:
    RandomNumberRange(int low, int high)
            : random_engine_{std::random_device{}()}
            , distribution_{low, high}
    {}
    double operator()()
    {
        return distribution_(random_engine_);
    }
private:
    std::mt19937 random_engine_;
    std::uniform_int_distribution<> distribution_;
};

void generate(std::vector<int> data, RandomNumberRange generator)
{
    return std::generate(std::begin(data), std::end(data), generator);
}

And this is my main function

int main()
{
    std::vector<int> volume(days);
    generate(volume, RandomNumberRange(1, 100));

    return 0;
}

I get this error :

Undefined symbols for architecture x86_64:
  "RandomNumberRange::RandomNumberRange(int, int)", referenced from:
      random_input(unsigned int const&) 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)

I want to populate a vector with random integers and include the code for this process in a separate file.

I think the problem is in the header file but I cannot figure out where.

uniqxx
  • 13
  • 4
  • 1
    [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). From there I'd worry about how you actually built your program. – WhozCraig Nov 30 '19 at 11:04
  • I removed the template which reduced the error. I edited my main post. – uniqxx Nov 30 '19 at 11:12
  • 1
    Do you see that you're completely redefining `class RandomNumberRange` in your .cpp file ? That file should include the header datagenerator.hpp, and then only implement whatever wasn't inlined in the header. There should only be one `class RandomNumberRange`, and it should be in the header. All of this is covered in any [book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). As you have it, I would strip the full def out of the .cpp and replace the one in the header with that. All the members are inlined anyway, so it needs to presence in the .cpp file. – WhozCraig Nov 30 '19 at 11:16

0 Answers0