-1

When using a function that requires use of the subclass, I have an error that said subclass hasn't been declared. So how would I declare this subclass without having an issue of redeclaration later?

This is a general idea of what the code would look like:

class MyClass {
public:
    void myFunction(Node* myNode);
private:
    class Node {
        public:
            Node();
            Node(string myString, int myInt);
            ~Node();
            string m_string;
    private:
        int m_int;
    }
};

So in this case, how would I declare Node so that it could be used in myFunction without redeclaring it later?

Monzaku
  • 57
  • 5
  • 3
    Step 1 in answering your question is [getting a good reference book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from. You can only declare a class once. You can't declare it later because that definition would conflict. – tadman Jan 31 '19 at 19:57
  • Put `class Node;` above `void myFunction(Node* myNode);` or change `void myFunction(Node* myNode);` to `void myFunction(class Node* myNode);` I don't think this is a terrible question once you figure out the issue. – drescherjm Jan 31 '19 at 20:00
  • @tadman You can declare things as many times as you like, provided there is only one definition. – François Andrieux Jan 31 '19 at 20:00
  • 2
    `Node` is not a "subclass". That word has another meaning. It is a *nested* class. – eerorika Jan 31 '19 at 20:06
  • @FrançoisAndrieux That's a better way of phrasing it. – tadman Jan 31 '19 at 20:28

1 Answers1

0

So how would I declare this subclass[nested class] without having an issue of redeclaration later?

By declaring (and defining) the nested class before declaring a member function with an argument that depends on the nested class declaration.

class MyClass {
private:
    class Node {
        public:
            Node();
            Node(string myString, int myInt);
            ~Node();
            string m_string;
    private:
        int m_int;
    }
public:
    void myFunction(Node* myNode);
};

While a forward declaration would be sufficient to declare a function with a pointer argument, nested classes cannot be forward declared.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    maybe worth mentioning that you cannot call `myFunction` when `Node` is private – 463035818_is_not_an_ai Jan 31 '19 at 20:17
  • @user463035818 That's a bit besides the point of the question, so I would be happy to let it stay in a comment. Besides, I'm not sure if there isn't some hack to get around that :) Probably not practically useful though. – eerorika Jan 31 '19 at 20:21
  • there is a hack, but I dont really understand it and opened a [question](https://stackoverflow.com/questions/54469015/why-is-it-allowed-to-deduce-a-private-type) – 463035818_is_not_an_ai Jan 31 '19 at 20:50
  • ***While a forward declaration would be sufficient to declare a function with a pointer argument, nested classes cannot be forward declared.*** Is that a bug with Visual Studio? I forward declare a nested private implementation for PIMPL all the time. Something like class myClass {private: class privateImpl; std::unique_ptr m_pPrivate;}; then in the cpp file I define myClass::privateImpl – drescherjm Jan 31 '19 at 21:09
  • @drescherjm https://stackoverflow.com/questions/951234/forward-declaration-of-nested-types-classes-in-c says so. There's a proposal though http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0289r0.pdf (which needs work). I haven't checked the rules whether this has "no diagnostic required" relaxation; if not, then not diagnosing it is technically violating the standard. – eerorika Jan 31 '19 at 21:13