文章目录

  • VScode C++ 编译error
    • 一、报错信息:
    • 二、文件结构:
    • 三、问题分析
    • 四、问题解决
      • 方法一:去掉多余的含入
      • 方法二:使用前置声明

VScode C++ 编译error

一、报错信息:

  1. ‘XXX’ was not declared in this scope
  2. template argument 1 is invalid

二、文件结构:

//A.h
#ifndef __A__
#define __A__
#include"B.h"
#include<vector>
class A{public:std::vector<B> myB;...
}
#endif
//B.h
#ifndef __B__
#define __B__
#include"A.h"
class B{...
}
#endif
//test.cpp
...A aa;
...

三、问题分析

如上所示,为了实现一些功能,我在两个头文件中分别include了另外一个头文件,使得他们互相包含,并且使用了#ifndef…这样的唯一定义标识,但在实现文件中创建了一个需要使用对象B的对象A,然后编译提示class B was not declare in this scope
原因是两个类在互相使用时必须保证对方先被声明,否则将会出现所谓的超前引用

四、问题解决

方法一:去掉多余的含入

若没有互相包含的必要,则只保留一个#include

//A.h
#ifndef __A__
#define __A__
#include"B.h"
#include<vector>
class A{public:std::vector<B> myB;...
}
#endif
//B.h
#ifndef __B__
#define __B__
//#include"A.h"    --删掉这一行,则可以正常编译
class B{...
}
#endif

方法二:使用前置声明

如果实在需要互相包含,则使用前置声明 class B

//A.h
#ifndef __A__
#define __A__
#include"B.h"
#include<vector>
class B;    //class B的前置声明
class A{public:std::vector<B> myB;...
}
#endif
//B.h
#ifndef __B__
#define __B__
#include"A.h"
class B{...
}
#endif

VScode C++ 编译error:‘XXX‘ was not declared in this scope, template argument 1 is invalid解决方法相关推荐

  1. c++编译 [Error] ‘revise‘ was not declared in this scope

    源码 #include <stdio.h> #include <string.h> #include <malloc.h> #include <windows ...

  2. Windows网络编程 c语言 报错:[Error] ‘bzero‘ was not declared in this scope

    阅读前请看一下:我是一个热衷于记录的人,每次写博客会反复研读,尽量不断提升博客质量.文章设置为仅粉丝可见,是因为写博客确实花了不少精力.希望互相进步谢谢!! 文章目录 阅读前请看一下:我是一个热衷于记 ...

  3. 解决Cygwin编译cocos2dx 遇到的 error: 'UINT64_C' was not declared in this scope 问题

    环境工具:Win10.VS2013.cocos2d-x-2.2.6.Cygwin.ADT 问题来源:写了一个小游戏,VS2013上运行成功,就尝试着打包apk,项目导入到ADT里面,添加了cocos2 ...

  4. 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope

    解决办法 出现错误:jni/ffmpeg/libavutil/common.h:175:47: error: 'UINT64_C' was not declared in this scope 解决: ...

  5. GVINS编译出现大量的error: ‘CV_CALIB_CB_ADAPTIVE_THRESH’ was not declared in this scope错误

    错误: /home/knight/GVINS_GI/src/GVINS-main/camera_model/src/chessboard/Chessboard.cc:38:43: error: 'CV ...

  6. 【C++】mingw32-make+cmake:error: ‘nullptr‘ was not declared in this scope解决方法

    ###问题 使用cmake成功(Configuring done Generating done)后,在cmd终端中执行 mingw32-make报错: error: 'nullptr' was no ...

  7. error: 'NULL' was not declared in this scope

    编译时出现这个错误: error: 'NULL' was not declared in this scope 解决方式: 在文件开头添加如下代码: #include <stddef.h> ...

  8. error: ‘ULONG_MAX’ was not declared in this scope

    在编译.cpp文件时,出现错误: error: 'ULONG_MAX' was not declared in this scope 解决方法:在.h文件中添加头文件,#include  <cl ...

  9. [Error]'cout'was not declared in this scope

    编译环境:Dev-C++ 5.11 操作系统:Windows 10 专业版 #include <iostream> #include <string>int main(){co ...

最新文章

  1. c# imager让图片有圆角unity_C# 10分钟完成百度图片提取文字(文字识别)——入门篇...
  2. ESLG.CommonUtility.NHibernateHelper的类型初始值设定项引发异常
  3. daily scrum 11.1
  4. 通过crontab调度java -jar任务提示nohup: failed to run command `java': No such file or directory...
  5. 王道考研——计算机组成原理(第二章 数据的表示和运算)
  6. 解析鸿蒙内核消息队列QueueMail接口的哼哈二将
  7. vrrp 理论和配置
  8. bae3.0第三步 添加默认管理后台模块和mysql库
  9. 最热web前端技术精粹
  10. matlab机器学习基础
  11. 数据仓库:金融/银行业的分层架构篇
  12. 【台州世通兰陵王系列】服务器的1U、2U、4U是指什么?
  13. 领航机器人广告段子_“满屏尽是段子手”,喜剧综艺如何从“笑声”中突围?...
  14. 怎么把应用隐藏到计算机里,怎么把桌面上的软件隐藏起来
  15. 做强信息产业 拓宽智慧城市发展路径
  16. 高通平台蓝牙--BLE 配置文件的日志示例
  17. Ubuntu QT 5.9.0 安装
  18. 新英汉翻译词典软件vb代码
  19. fidder+天天模拟器 配置模拟器抓包
  20. canvas实现雨滴动画

热门文章

  1. SQL Server 2005系列教学(1) SQL SERVER2005介绍及安装
  2. 进程创建与撤销的模拟实现
  3. 按年/月/日统计数据
  4. 【史纲】近现代史纲要知识说明
  5. Unity3D学习日记3
  6. Linux0.11内核源码解析01
  7. K线形态识别_下探上涨
  8. 基于Face++使用python+opencv来实现人脸验证及人脸解锁
  9. Qt下的视频实时传输
  10. 安装vue浏览器插件-Vue.js devtools