Error does not name a type C++

●2 ●7
calendar_today • schedule1 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.

Want more #cplusplus & #c deep dives?

Join 6,035+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Let's play with C++ reflection

Jakub Neruda - Nov 12

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

How to Debug Failed n8n Workflows: Step-by-Step Guide

floxolab - Nov 2

n8n Error Workflow Alerts: Telegram, Slack, Email, or Chat

floxolab - Nov 5
chevron_left
174 Points • 9 Badges
3Posts
0Comments
I am full stack developer

Commenters (This Week)

12 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!