diff --git a/compiler2/Type_codegen.cc b/compiler2/Type_codegen.cc
index 8d44ec3ba5fe620c64c6fd923a49f0a4790ddded..8527e2f21533fd5b47d3c2e9f0dc0657b6525be1 100644
--- a/compiler2/Type_codegen.cc
+++ b/compiler2/Type_codegen.cc
@@ -1173,7 +1173,21 @@ void Type::generate_code_Choice(output_struct *target)
     CompField *cf = get_comp_byIndex(i);
     const Identifier& id = cf->get_name();
     Type *cftype = cf->get_type();
-    sdef.elements[i].type = pool.add(cftype->get_genname_value(my_scope));
+    string type_name = cftype->get_genname_value(my_scope);
+    if (T_ANYTYPE != typetype &&
+        cftype->get_my_scope()->get_scope_mod_gen() == my_scope->get_scope_mod_gen()) {
+      for (size_t j = 0; j < sdef.nElements; ++j) {
+        if (get_comp_byIndex(j)->get_name().get_name() == type_name) {
+          // the field's type name clashes with the name of a field in this union,
+          // which would cause a C++ compilation error
+          // always prefix the type with the namespace in this case
+          type_name = my_scope->get_scope_mod_gen()->get_modid().get_name() +
+            string("::") + type_name;
+          break;
+        }
+      }
+    }
+    sdef.elements[i].type = pool.add(type_name);
     sdef.elements[i].typedescrname =
       pool.add(cftype->get_genname_typedescriptor(my_scope));
     sdef.elements[i].typegen = pool.add(cftype->get_genname_xerdescriptor());
@@ -1571,7 +1585,20 @@ void Type::generate_code_Se(output_struct *target)
     CompField *cf = se_comps[i];
     const Identifier& id = cf->get_name();
     Type *type = cf->get_type();
-    cur.type    = pool.add(type->get_genname_value(my_scope));
+    string type_name = type->get_genname_value(my_scope);
+    if (type->get_my_scope()->get_scope_mod_gen() == my_scope->get_scope_mod_gen()) {
+      for (size_t j = 0; j < sdef.nElements; ++j) {
+        if (se_comps[j]->get_name().get_name() == type_name) {
+          // the field's type name clashes with the name of a field in this record/set,
+          // which would cause a C++ compilation error
+          // always prefix the type with the namespace in this case
+          type_name = my_scope->get_scope_mod_gen()->get_modid().get_name() +
+            string("::") + type_name;
+          break;
+        }
+      }
+    }
+    cur.type    = pool.add(type_name);
     cur.typegen = pool.add(type->get_genname_own());
     cur.of_type = type->get_type_refd_last()->is_seof();
     cur.typedescrname =
diff --git a/regression_test/compileonly/Makefile b/regression_test/compileonly/Makefile
index 34a50e9d6a642cbfbdf2cadd7ca08ccb8b499d4d..e39b1df8da00f8a23982f2287fae5ea9fd8117b3 100644
--- a/regression_test/compileonly/Makefile
+++ b/regression_test/compileonly/Makefile
@@ -28,7 +28,7 @@ CODIRS := dynamicTemplate styleGuide topLevelPdu \
 	isbound namedActualParameters assignmentNotation \
 	attribQualif HT48786 selectCase openTypeNames \
 	defaultParamUsageBeforeDecl deterministic readFromFile \
-	asn1_hyphen
+	asn1_hyphen nameClash
 
 all dep clean distclean:
 	for dir in $(CODIRS); do $(MAKE) -C $$dir $@ || exit; done
diff --git a/regression_test/compileonly/nameClash/Makefile b/regression_test/compileonly/nameClash/Makefile
new file mode 100644
index 0000000000000000000000000000000000000000..b9fd232646f039c46b43c0e499fcb936dc575804
--- /dev/null
+++ b/regression_test/compileonly/nameClash/Makefile
@@ -0,0 +1,50 @@
+##############################################################################
+# Copyright (c) 2000-2016 Ericsson Telecom AB
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#   Botond, Baranyi
+#
+##############################################################################
+TOPDIR := ../..
+include $(TOPDIR)/Makefile.regression
+
+.PHONY: all clean dep
+
+TTCN3_LIB = ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX)
+
+TTCN3_MODULES = nameClash.ttcn
+
+GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.cc)
+GENERATED_HEADERS = $(GENERATED_SOURCES:.cc=.hh)
+ifdef CODE_SPLIT
+GENERATED_SOURCES := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), .cc _seq.cc _set.cc  _seqof.cc _setof.cc _union.cc))
+else ifdef SPLIT_TO_SLICES
+POSTFIXES := $(foreach file, $(SPLIT_TO_SLICES), $(addsuffix $(file), _part_))
+POSTFIXES := $(foreach file, $(POSTFIXES), $(addprefix $(file), .cc))
+GENERATED_SOURCES2 := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), $(POSTFIXES)))
+GENERATED_SOURCES += $(GENERATED_SOURCES2)
+endif
+
+OBJECTS = $(GENERATED_SOURCES:.cc=.o)
+
+TARGET = nameClash$(EXESUFFIX)
+
+all: $(TARGET)
+
+$(TARGET): $(GENERATED_SOURCES) $(USER_SOURCES)
+	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ -L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) \
+	-L$(OPENSSL_DIR)/lib -lcrypto $($(PLATFORM)_LIBS)
+
+$(GENERATED_SOURCES) $(GENERATED_HEADERS): $(TTCN3_MODULES)
+	$(TTCN3_COMPILER) $(COMPILER_FLAGS) $^
+
+clean distclean:
+	$(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
+	$(GENERATED_SOURCES) compile *.log
+
+dep: $(GENERATED_SOURCES)
+	makedepend $(CPPFLAGS) $(GENERATED_SOURCES)
diff --git a/regression_test/compileonly/nameClash/nameClash.ttcn b/regression_test/compileonly/nameClash/nameClash.ttcn
new file mode 100644
index 0000000000000000000000000000000000000000..681f83bafc132b149f94acfa04802dac69ec090e
--- /dev/null
+++ b/regression_test/compileonly/nameClash/nameClash.ttcn
@@ -0,0 +1,40 @@
+/******************************************************************************
+ * Copyright (c) 2000-2016 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Baranyi, Botond
+ *
+ ******************************************************************************/
+
+// This module contains tests for clashes between record/set/union field names
+// and the name of its type (or another field's type in the same record/set/union).
+// These cases used to cause C++ compilation errors.
+
+module nameClash {
+
+type record of integer il;
+type record of integer il2;
+
+// in this record both fields' names clash with each other's type names
+type record R {
+  il il2,
+  il2 il
+}
+
+// in this set the field's name clashes with its own type name
+type set S {
+  il2 il2
+}
+
+// in this union the first field's name clashes with the 3rd field's type name
+type union U {
+  integer il,
+  R x,
+  il y
+}
+
+}