How to create a constructor in source file C++
Solution 1:
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