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

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐