【c++ debug】error:xxx is not a member of ‘xxx‘, no matching function...跨命名空间使用结构体

55 篇文章 3 订阅
14 篇文章 1 订阅
本文详细介绍了在C++编程中遇到的头文件循环依赖导致的错误,如'xxx'不在指定命名空间中。分析了原因并提出了解决方案,包括去除循环依赖和使用using声明。同时,讨论了using引用提前声明变量的方法及其局限性。
摘要由CSDN通过智能技术生成

1. 报错

  • in file included from …/header_file1.h, from …/source_file1.cc
    …/header_file2.h: error: ‘xxx’ is not a member of ‘namespace::class’

  • …/source_file1.cc: error:no matching function for call to 'source_file2.cc member function(xxx arg)’

  • 跨命名空间使用结构体,头文件有循环依赖

======== header_file1.h ========

#include "header_file2.h"
...

namespace1 {
	struct MyStruct {
		...
	};
}

======== header_file2.h ========

#include "header_file1.h"
...

member function(vector<namespace1::MyStruct>& arg);

  • header_fle1.h和header_file2.h循环依赖,source_file2.cc函数使用了header_file1.h中变量,source_file1.cc中又调用了source_file2.cc中的函数

2. 原因

  • 头文件循环依赖,header_file1.h包含头文件header_file2.h的时候发现使用的变量还未声明
  • header_file2.h中使用 using namespace1::class_xxx 也不管用,仍提示找不到结构体

3. 解决方法

  • 去除头文件循环依赖

    或者

  • 使用支持using引用提前声明变量的方式

参考文章:
类似报错1:回答使用using但未解决

类似报错2: 回答说利用委托

created by shuaixio, 2021.11.22

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值