user3424033
user3424033

Reputation:

Forward declaration of a template class c++

I saw similar examples, but didn't understand them fully so please don't mark this as duplicate straight away. I think there's a simple solution for my problem, and I'm only learning C++.

I want to use:

template<class T, std::size_t N>
class arnas_array {
//a copy of std:array functionality, basically, here.
};

in another class header, another file, example:

class options_databaze {

public:

    struct options_to_save{
        arnas_array<char, 123> option_name;
        //char option_name[103];
        int * option_value_pointer; 
    };


};

And I can't get it to work. Forward declaration like this won't work:

template<class T, std::size_t N>
class arnas_array;

I don't know much about this problem, first time I'm stuck here, any examples are gold.

error C2079: 'options_databaze::options_to_save::option_name' uses undefined class 'arnas_array<char,123>'

Upvotes: 0

Views: 2332

Answers (1)

Anton Savin
Anton Savin

Reputation: 41331

The question has nothing to do with templates. In C++ a class type T must be complete, in particular, if a non-static class data member of type T is declared (see 3.2/5 (One definition rule) section of the standard, or read more human-readable version here).

"Must be complete" means that the definition of the class T should precede the definition of the corresponding data member. A common way to achieve this, as was pointed out by Cameron in the comments, is to put a definition in a header file and include that header everywhere it's needed - just the same way as you do when you include standard headers such as <vector>, <map> and so on.

Upvotes: 1

Related Questions