-1

I'm trying to pass a reference to a fixed-sized C-style array to a constructor. Here is my code:

#include <cstddef>

template <typename T, size_t size>
class Foo
{
public:
    Foo(T (&arr)[size]);

    T (&m_arr)[size];
    size_t m_size;
};

Foo::Foo(T (&arr)[size])
  : m_arr {arr},
    m_size {size}
{}

Here are the build errors when trying to compile the above:

invalid use of template-name 'Foo' without an argument list
expected unqualified-id before ',' token
expected constructor, destructor, or type conversion before '{' token
expected unqualified-id before '{' token

Using the STL is not an option.

I'm using GNU v7.3.1 (Linaro).

sifferman
  • 2,955
  • 2
  • 27
  • 37
  • That's not the proper syntax for defining members of class templates. – molbdnilo Apr 28 '23 at 06:32
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to "search and then research" and you'll find plenty of related SO posts for this. – Jason Apr 28 '23 at 06:50

1 Answers1

3

You're missing the template parameters when defining (implementing) the Foo constructor:

template<typename T, size_t size>
Foo<T, size>::Foo(T (&arr)[size]) ...

Quite honestly, any decent beginners learning resource should have that information.


A perhaps easier solution would be to define the constructor inline in the class itself:

template<typename T, size_t size>
class Foo
{
public:
    Foo(T (&arr)[size])
        : m_arr{ arr }, m_size{ size }
    {
    }

    // ...
};

By the way, you don't need the m_size member, as you already have the size template argument.

sifferman
  • 2,955
  • 2
  • 27
  • 37
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621