added check for presence of required fields. (flatc) c++ (#4316)

* added check for presence of required fields.

* updates to resolve Travis CI build error.

* fixes for resolving appveyor build errors.

* fixes for resolving appveyor build errors.

* fixes for resolving appveyor build errors.

* updates per aardappel's comments.

* updated a variable's name.

* updates per aardappel's comments.
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp
index 78072b9..b813491 100644
--- a/src/idl_parser.cpp
+++ b/src/idl_parser.cpp
@@ -897,6 +897,29 @@
     EXPECT(',');
   }
 
+  // Check if all required fields are parsed.
+  for (auto field_it = struct_def.fields.vec.begin();
+            field_it != struct_def.fields.vec.end();
+            ++field_it) {
+    auto required_field = *field_it;
+    if (!required_field->required) {
+      continue;
+    }
+    bool found = false;
+    for (auto pf_it = field_stack_.end() - fieldn;
+         pf_it != field_stack_.end();
+         ++pf_it) {
+      auto parsed_field = pf_it->second;
+      if (parsed_field == required_field) {
+        found = true;
+        break;
+      }
+    }
+    if (!found) {
+      return Error("required field is missing: " + required_field->name + " in " + struct_def.name);
+    }
+  }
+
   if (struct_def.fixed && fieldn != struct_def.fields.vec.size())
     return Error("struct: wrong number of initializers: " + struct_def.name);