0

What I'm aiming for, Is that I would like a function template to convert a vector to a type* array for ease of use. Instead of creating multiple functions for the "same" functionality.

With following code(Note function is inside namespace Khan):

// Func.h
template <typename T>
T* vector_to_array(std::vector<T> vec);

// Func.cpp
template <typename T>
T* vector_to_array(std::vector<T> vec)
{
    T* arr = new T[vec.size()];
    std::copy(vec.begin(), vec.end(), arr);

    return arr;
 }

// Inside main function
std::vector<int> test_vec = { 0, 2, 3 };
int* test_arr = Khan::vector_to_array(test_vec);

I get the following error:

undefined reference to Khan::vector_to_pointer(std::vector<int, std::allocator<int> >)

I tested different ways, and made a specific function for an int as in the example above excluding the template.

Thus my question is, what is the plausible cause of these errors, and what would be a possible fix?

And to clarify my goal. What my goal is for this, is for the function not dependent on the type, to return a copy of a vector in a C-styled array.

*Edit: Rewrote with complete code, and parts of error was missing.

Solved: As this question is a duplicate, I found my answers in the other post. To summarise what caused this issue, the previous stated error was the cause of declaring the template in a ".cpp" file. And was fixed with declaring and defining the template inside the header file.

Linden
  • 1
  • 4
  • Note that `vector_to_array` makes two copies. The one it returns and when `test_vec` is copied to `vec`. – François Andrieux Nov 06 '18 at 19:26
  • 3
    Please provide a [MCVE] that produces the error. The [code you've shared](https://godbolt.org/z/QWFuIq) does not produce the problem you describe. – François Andrieux Nov 06 '18 at 19:27
  • It's not entirely clear from what you've shown, but is the implementation of `vector_to_array` maybe in a .cpp source file? – François Andrieux Nov 06 '18 at 19:28
  • @FrançoisAndrieux Noted. I'll look it up, as I assume you mean there's a way of only copying it once. – Linden Nov 06 '18 at 19:29
  • It seems your intention was to make `vec` a `const` reference (`const std::vector & vec`) so that `vec` will refer to the argument given instead of taking it's value by copy. – François Andrieux Nov 06 '18 at 19:30
  • Your error states `vector_to_pointer` is missing, but your function is called `vector_to_array`. – yyny Nov 06 '18 at 19:30
  • What is Khan? Where is the corresponding code? Please post a [mcve]. – Jean-Baptiste Yunès Nov 06 '18 at 19:37
  • @Jean-BaptisteYunès Sorry, not used to asking formated questions here. It looks like the post is updated now. Khan is the namespace where the function is located inside. – Linden Nov 06 '18 at 19:41
  • I've got to ask why. You know you can get a pointer to the managed buffer, right? – Lightness Races in Orbit Nov 06 '18 at 20:16
  • @LightnessRacesinOrbit No I did not, but as I wanted a copy of the contents of the array It felt appropriate with a simple copy. But thank you for pointing that out! – Linden Nov 06 '18 at 20:32
  • It's just that your copy is unmanaged and has unclear ownership semantics. Why not copy the _vector_ then access the copy's buffer when you need it? You're not really gaining anything here except the need to later call `delete[]` yourself, and I can think of very few (not none, but very few) scenarios in which you have the need (or even need the ability) to do that. (Perhaps a third-party library has already promised to do it?) – Lightness Races in Orbit Nov 06 '18 at 20:39

0 Answers0