How to create a constructor in source file C++

I have been researching for hours. This simple task is eluding me...

Any suggestions of refactoring are encouraged. I am not a C++ person obviously.

I've watched all these videos but none of the demo classes have multiple fields. I certainly have not seen a source file that initializes a null array.

Links:
C++ - Classes - Creating Source files and Header files

Buckys C++ Programming Tutorials - 15 - Placing Classes in Separate Files

C++ Header Files*

How the C++ Linker Works

Constructors in C++

How to Write a C++ Class

Separating a C++ class into a .h and .cpp files

After a few comments I have updated the context of the question

Node.h

#ifndef NODE_H_                                /* INCLUDE GUARD */
#define NODE_H_ 
#include <iostream>

namespace Node{
  class Node {
  private:                                       // PRIVATE FIELDS
    static const int size = 27;

  public:                                        // PUBLIC FIELDS
    Node(bool isWord);                           // CONSTRUCTOR

    bool isWord;
    Node* character[size]{};

    void insertme(std::string);                  // FUNCTION PROTOTYPE
    int  searchme(std::string);                  // FUNCTION PROTOTYPE
  };
}

#endif                                           // NODE_H_

Node.cpp

// SOURCE FILE

#include "Node.h"
#include <iostream>

using namespace std;

Node::Node(bool isWord) {
    /*
     * This constructor needs to:
     * set isWord to false
     * populate Node* character[size] to be filled with null
     */
};

void insertme(string token){
    return;
}

int searchme(string token){
    return 0;
}

NOTE: this constructor does not throw any errors but it doesn't initialize the member fields the way I need it to

// SOURCE FILE

#include "Node.h"
#include <iostream>

using namespace std;

Node::Node(isWord) {};

void insertme(string token){
    return;
}

int searchme(string token){
    return 0;
}

2 answers

  • answered 2022-01-23 03:01 h0tst3w

    In your header file, you've created a function prototype without a variable.

    public:                                        // PUBLIC FIELDS
    Node();                                      // CONSTRUCTOR
    

    This will make the class use the default constructor which does nothing.

    Instead, you need to define a constructor with a boolean parameter in the header file.

    public:                                        // PUBLIC FIELDS
    Node();                                      // CONSTRUCTOR
    Node(bool);                                  // CONSTRUCTOR 2
    

    You will also need to make sure you include the proper type in the function definition of the Node constructor in the .cpp file.

      Node::Node(bool isWord)  {
        // DO SOMETHING.
        };
    

  • answered 2022-01-23 03:46 Ted Lyngmo

    You need to declare a default value for your constructor taking a bool if you want it to be usable without arguments. A constructor without any mandatory arguments is a default constructor.

    The definition of the class and declaration of its member functions:

    #pragma once // or a standard header guard
    
    class Node {
    public:
        Node(bool isWord = false); // this is now a default constructor
    
        // other members etc ...
    };
    

    A possible use of it:

    #include "Node.h"              // where Node is defined
    
    int main() {
        Node x;                    // default construct a `Node`
    }
    

    The definition of the constructor in Node.cpp:

    #include "Node.h"
    #include <ios>
    #include <iostream>
    
    Node::Node(bool isWord) {      // note, no default values here
        std::cout << std::boolalpha << isWord << '\n';
    }
    

    Output if compiled and linked:

    false
    

    Demo

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum