1

I am coding struct Node and I think this is constructor initializer error. What did I write wrong?

class linkedlist 
{
private:
    struct Node
    {
        char m_num;
        char m_logo;
        char m_name;
        char m_1;
        int m_2;
        Node* next;
    };
    Node() 
        : m_num(), m_logo(), m_name(), m_1(), m_2(), next(nullptr) // error: expected unqualified-id before ')' token|
    {}
    Node(const char& a, const char& b, const char& c, const char& d, const int& e) 
        : m_num(a), m_logo(b), m_name(c), m_1(d), m_2(e), next(nullptr) // error: expected unqualified-id before 'const'|
    {}                                                                  // error: expected ')' before 'const'| 

    Node* head = nullptr;
    Node* tail = nullptr;
JeJo
  • 30,635
  • 6
  • 49
  • 88
Jacky Teerapat
  • 125
  • 1
  • 10

1 Answers1

3

You need to move your Node constructors inside the definition of Node class.

struct Node
{
    char m_num;
    char m_logo;
    char m_name;
    char m_1;
    int m_2;
    Node* next;
    Node() 
        : m_num(), m_logo(), m_name(), m_1(), m_2(), next(nullptr)
    {}
    Node(const char& a, const char& b, const char& c, const char& d, const int& e) 
        : m_num(a), m_logo(b), m_name(c), m_1(d), m_2(e), next(nullptr)
    {}
};

or you need to declare the constructors inside the Node class defenition beforehand.

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Thank you. Can you show me what is the other way? (declare what? ) – Jacky Teerapat Jul 20 '19 at 06:21
  • @JackyTeerapat It needs the `Node` as public. I wouldn't go for that as `Node` is a `private` property of `linkedlist`. However here is it: https://wandbox.org/permlink/MuzBDbQ3WBmCFLGd – JeJo Jul 20 '19 at 06:24
  • @JackyTeerapat Also note that **the primitive types can be passed by value than ref**:https://stackoverflow.com/questions/14013139/is-it-counter-productive-to-pass-primitive-types-by-reference. That means. `Node` 's second constructor could be simply: `Node(char a, char b, char c, char d, int e)` – JeJo Jul 20 '19 at 06:34
  • Do you mean to change anything to the public? @JeJo – Jacky Teerapat Jul 20 '19 at 06:38
  • **"the primitive types can be passed by value than"** It means I can't pass by ref? Does it work the only method? – Jacky Teerapat Jul 20 '19 at 06:41
  • @JackyTeerapat Yes. Looks like `Node` has to be an internal type of class `linkedlist`, and hence, my the second option is not a good idea. – JeJo Jul 20 '19 at 06:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196736/discussion-between-jejo-and-jacky-teerapat). – JeJo Jul 20 '19 at 06:42