0

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>'
5
  • 2
    Your forward declaration is actually perfectly fine, however you cannot use a forward declared class to declare a member of that type until the class is fully defined (because otherwise the compiler would have no way of determining the size of the member and thus the size of struct options_to_save). You can declare a member that's a pointer to that type, however (data pointers are all the same size regardless of their type). (And you're missing a ; at the end of your class definition, but I'm sure that's just a copy-paste error.) Commented Dec 5, 2014 at 22:11
  • how should i avoid forward declarations then and use same class in every file ? i wouldnt like to change my code to use pointers, im editing application, not writing new code in this situation Commented Dec 5, 2014 at 22:14
  • 2
    As a tip, always include the actual error messages you get when trying to compile the code when posting Commented Dec 5, 2014 at 22:15
  • error C2079: 'options_databaze::options_to_save::option_name' uses undefined class 'arnas_array<char,123>' Commented Dec 5, 2014 at 22:17
  • @Arnas: You can put the definition of arnas_array in a header (I'm assuming that's already the case since it's templated) and include that header before you define options_databaze. Commented Dec 5, 2014 at 22:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, since this class was already in header(which had all the defines, and other 2 small classes, its like a "main" header of thingies) i tought that's not the case, since i had that header included. But seems like seperate header with arnas_array class defined fixed the things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.