Error does not name a type C++

posted 3 min read

Error does not name a type in C++

The "error does not name a type" in C/C++ is defined as the when user declares outside of the function or does not include it properly in the main file this error will through.

TABLE OF CONTENTS
  1. Why this error occurs?
  2. How to resolve:
    1. Solution 1:
    2. Solution 2:
    3. Solution 3:
  3. Conclusion

Why this error occurs?

File: sample.h  
 #ifndef SAMPLE_H__<br />#define SAMPLE_H__
   namespace Sampletest {
    class CP_M_ReferenceCounted<br />{<br />};
    }
    #endif
    <br />File : test.cpp
#include "sample.h"
    typedef CP_M_ReferenceCounted FxRC;
   <br />int main(int argc, char **argv)<br />{
    <p>return 0;<br />}


If you run the above code you will get the error "...error: CP_M_ReferenceCounted does not name a type"

How to resolve :

To overcome this error follow this two methods of solutions

Solution 1:

To avoid this error, we have to add the "using namespace Sampletest" at the start of the program in C++.

To avoid this error, we have to add the "using namespace Sampletest" at the start of the program in C++.

<p>File : test.cpp</p>
<p><code>#include "sample.h"</p>
<p>//Add the following to your code</p>
<p>using namespace Sampletest</p>
<p>typedef CP_M_ReferenceCounted FxRC;</p>
<p><br />int main(int argc, char **argv)<br />{</p>
<p>return 0;<br />}</code></p>

Solution 2:

To avoid this kind of error we need to add the header "#include sample.h" at the starting of the program in C++.

Same Problem while declaring outside of the function

These kinds of errors are also acquired while printing outside of any function.
A statement which isn't declaration in C++ need to be printed inside of the function


File test.cpp

<p>#include <iostream><br />#include <cstring><br />using namespace std;</p>
<p>struct Node{<br />char *name;<br />int age;<br />Node(char *nc = "", int ax = 0){<br />name = new char[strlen(nc) + 1];<br />strcpy(name, n);<br />age = ax;<br />}<br />};</p>
<p>Node node1("Roger", 20), node2(node1);<br />cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;<br />strcpy(node2.name, "Wendy");<br />node2.name = 30;<br />cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;</p>
<p> </p>
<p><b>Output :</b></p>
<p>error: ‘node2’ does not name a type</p>
<br/>

Solution 3:

To avoid these types of errors you need to add everything inside the function,
because a statement that isn't a declaration in C++ needs to be inside the function

<p><code>int main() {<br />Node node1("Roger", 20), node2(node1);<br />cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;<br />strcpy(node2.name, "Wendy");<br />node2.name = 30;<br />cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;<br />}</code></p>

Conclusion:

In this article, we have seen why the undefined error occurs in C/C++ and discussed how to resolve these errors in various methods.

More Posts

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

ERROR (expected nested-name-specifier before ‘namespace’)-Fixed

Maroun - Mar 29

Tuesday Coding Tip 05 - Object initialization in C++

Jakub Neruda - Mar 31

Tuesday Coding Tip 08 — Explicit template specialization

Jakub Neruda - Apr 21
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

4 comments
2 comments

Contribute meaningful comments to climb the leaderboard and earn badges!