c++ class “xxxx“ does not name a type是怎么一回事

declare class does not name a type

出现这个编译错误主要有四个可能原因,现总结如下:

1.引用的类命名空间未包含

2.引用的类头文件未包含

3.包含了头文件,或者已经前置声明了,则说明所引用的类名写错。

4.循环引用头文件

前置声明要素:

1.前置声明需要注意以上提到的四点

2.尽可能的采用前置声明(做到只有包含继承类的头文件)

3.使用前置声明时,cpp文件中include 头文件次序必须先 包含前置声明的类定义头文件,再包含本类头文件。

否则会出现如下编译错误.

(expected constructor, destructor, or type conversion before ‘typedef')

https://blog.csdn.net/typename/article/details/7173550

 

原因二  前置声明

XXX应该是一种用户定义的数据类型,而由于没有声明或者拼写错误或者与关键词重名,导致编译有错,出现类型错误。

在一个源文件中,要声明或定义一个类的指针时,必须在使用前声明或定义该类,因此下面的代码会报错:class A{public:    B *b;};class B{public:    A *a;};int main(){    return 0;}12345678910111213141516

报错为“error: ‘B’ does not name a type”,就是因为在A类中使用B *b之前没有声明或定义B类,如果在第一行加上一句前置声明(forward declaration)“class B;”,就不会有这样的问题了。

 

class appv_forward;    //forward declaration  前置声明

class obu_data_channel_t              {
public:

 而在头文件互相包含时,也会引发“error: ‘xxx’ does not name a type”,其报错原因和上面的代码是相同的

请看下面的代码:a.h:

#ifndef A_H_INCLUDED

#define A_H_INCLUDED

#include "b.h"

class A{

public:  

   B *b;

};

#endif // A_H_INCLUDED

b.h:
 

#ifndef B_H_INCLUDED

#define B_H_INCLUDED

#include "a.h"

class B{

public:  

 A *a;

};
#endif // B_H_INCLUDED
main.cpp:

#include "a.h"

#include "b.h"

int main()

{  

    return 0;
}

 

编译就会报错:“error: ‘A’ does not name a type”。

预处理命令为“gcc -E -o a.i a.h”:

# 1 "a.h"# 1 "<built-in>"# 1 "<command-line>"# 1 "a.h"# 1 "b.h" 1# 1 "a.h" 1# 5,"b.h" 2class B{public: A *a;};

# 5 "a.h" 2class A{public: B *b;};1234567891011121314151617181920212223242526。

  • 15
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life. Extending the overall content by about 50%, the book now also covers all the new C++11 library components, including Concurrency Fractional arithmetic Clocks and Timers Random numbers and distributions New smart pointers Regular expressions New STL containers, such as arrays, forward lists, and unordered containers New STL algorithms Tuples Type traits and type utilities
In A Tour of C++, Second Edition, Bjarne Stroustrup, the creator of C++, describes what constitutes modern C++. This concise, self-contained guide covers most major language features and the major standard-library components—not, of course, in great depth, but to a level that gives programmers a meaningful overview of the language, some key examples, and practical help in getting started. Stroustrup presents the C++ features in the context of the programming styles they support, such as object-oriented and generic programming. His tour is remarkably comprehensive. Coverage begins with the basics, then ranges widely through more advanced topics, including many that are new in C++17, such as move semantics, uniform initialization, lambda expressions, improved containers, random numbers, and concurrency. The tour even covers some extensions being made for C++20, such as concepts and modules, and ends with a discussion of the design and evolution of C++. This guide does not aim to teach you how to program (for that, see Stroustrup’s Programming: Principles and Practice Using C++, Second Edition), nor will it be the only resource you’ll need for C++ mastery (for that, see Stroustrup’s The C++ Programming Language, Fourth Edition, and recommended online sources). If, however, you are a C or C++ programmer wanting greater familiarity with the current C++ language, or a programmer versed in another language wishing to gain an accurate picture of the nature and benefits of modern C++, you can’t find a shorter or simpler introduction than this tour provides.
### 回答1: 这个错误的意思是编译器无法识别该类型名称。通常是因为您在编写代码时使用了一个未声明或未定义的类型名称。例如,如果您试图在没有先声明或定义的情况下使用自定义类型,编译器就会出现这个错误。 解决方案包括: -检查并更正类型名称的拼写 -检查是否缺少包含该类型定义的头文件或库文件 -检查是否在使用前先声明或定义了该类型 这个错误常出现在编写C/C++程序时,因为它们是编译语言而不是解释性语言。 ### 回答2: "does not name a type" 是C++语言中的一个错误信息。通常提示错误的原因是在程序中使用了一个未被声明或定义的标识符作为了类型。下面简单解释一下这个错误信息。 在C++中,我们需要先声明一个类型(比如类、结构体或枚举),之后才能使用它创建变量、定义函数的参数或者返回值等。如果使用了未被声明或定义的类型,系统无法识别它并分配内存,就会报错。而“does not name a type”说明系统在编译期间无法识别这个标识符,即这个标识符不是一个类型名。 常见导致“does not name a type”报错的原因有以下几种: 1. 标识符未被正确声明或定义。 2. 标识符被误用了,比如把一个变量名当成了一个类名。 3. 标识符命名方式错误,比如使用了C++中的关键词或者命名与C++标准库中的名称重复。 4. 头文件没有被正确引入。 针对这些原因,我们可以进行相应的解决方案。对于第1个原因,我们应当检查是否有声明或定义该类型的代码;对于第2个原因,我们要检查是否有意外在不同作用域下使用了标识符;对于第3个原因,我们需要避免使用C++中的关键词或标准库名称作为变量名和类名等;对于第4个原因,我们需要在代码中明确引入相应的头文件。 总之,“does not name a type”错误信息是C++语言中很常见的错误提示,需要我们去仔细检查代码并找出其中的问题。一般情况下,根据代码的实际情况进行错误分析和处理即可解决这个问题。 ### 回答3: "does not name a type"是一个经常出现在编译器错误信息中的提示。这个错误通常是由于代码中的数据类型没有被声明或定义而引起的。 在编程过程中,我们必须在使用变量或函数之前将它们声明或定义。如果没有这样做,编译器就无法识别它们,并会提示“does not name a type”的错误。 例如,在C++中, ``` int main(){ foo(); return 0; } void foo(){ cout << "Hello World!" << endl; } ``` 这段代码中,函数foo()在调用前没有被声明,因此编译器无法识别它,会报错“foo() does not name a type”。 为了解决这个问题,我们可以将函数放在调用它的代码之前声明,如下所示: ``` void foo(); int main(){ foo(); return 0; } void foo(){ cout << "Hello World!" << endl; } ``` 这次编译无误。 除了函数,变量也应该在使用之前被声明或定义。这样可以确保编译器能够正确地识别它们,并避免类似“does not name a type”的错误提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aFakeProgramer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值