1

I'm having this declaration of a "relation" class which requires to be initialized "relation_label" value.

namespace hr {
    class node;

    enum relation_label : uint8_t {
        HAS, CONTAINS, IS_PARALLEL, INCLUDES, IS, PART_OF, LEADS_TO, IN
    };


    template<typename N>
    class relation : public std::vector<N> {
    private:
        relation(const relation &) = default;

    public:
        const relation_label label;

        relation(relation_label l) : std::vector<N>(0) {
            this->label = l;
        };
    };
}

This code compiles, but making new instances of relation objects does not compile. For example, the error on:

relation<word *> includes(relation_label::INCLUDES);

is "No type named 'INCLUDES' in 'hr::relation_label'".

Any idea? Thanks a lot.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
Luc
  • 21
  • 1
  • 5

2 Answers2

1

The error you're seeing could be caused by another error before that, which is:

const members must be initialized inline or with a member-init-list. In the body of the constructor they are already const.

Change

    relation(relation_label l) : std::vector<N>(0) {
        this->label = l;
    };

To

    relation(relation_label l) : std::vector<N>(0), label(l) {
    }

Also, I hope word is a type defined somewhere in your application.


=== EDIT ===

From the comment:

"word" is defined like this:

class word;
class word_related : public named_node {
public: relation<word*> includes(relation_label::INCLUDES);
};

What does relation<word*> includes(relation_label::INCLUDES) mean? It looks like a definition of a member function includes(), but then relation_label::INCLUDES must be a type. Which it is not, obviously.

Maybe you meant

relation<word*> includes(relation_label lbl = relation_label::INCLUDES);

?

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • I did what you did. No change unfortunately. :( I also removed the "const" qualifier, but still no change. "word" is defined like this: class word; class word_related : public named_node { public: relation includes(relation_label::INCLUDES); – Luc Apr 06 '20 at 20:20
0

As you're trying to create a new instance of the relation object in the line yielding the compile error, try using bracket-initialization

relation<word*> includes{relation_label::INCLUDES};

instead. When compiling your previous code

class word_related : public named_node {
public: relation<word*> includes(relation_label::INCLUDES);
};

the compiler thinks you're about to declare a member function called includes of the class named_node (and therefore it expects the type of the function's parameter instead of a 'value'), instead of defining a default-initialized member variable.

This answer is supplemental to rustyx's answer, who addresses the most important point of the missing initialization of the const member variable.

The code can be found here, with the fix from rustyx's answer applied.

mutableVoid
  • 1,284
  • 2
  • 11
  • 29