C++:将 this 指针传递给另一个类

标签 c++ pointers parameter-passing this member

我是 C++ 的新手,但我确实有一些 Java 经验。 在编码时,我偶然发现了一个让我困惑的错误。 这是我的代码(经过简化,但错误相同):

A.h:

#pragma once
#include "B.h"
class A
{
public:
    A();
    void foo();
    void sayHello();
    B b;
 };

A.cpp:

#include "A.h"
#include <iostream>    
A::A() {}
void A::foo() {
    b.bar(this);
}
void A::sayHello() {
    std::cout << "Hello" << std::endl;
}

B.h:

#pragma once
#include "A.h"
class B
{
public:
    B();
    void bar(A *a);
};

B.cpp:

#include "B.h"
B::B(){}
void B::bar(A *a) {
    a->sayHello();
}

我想将 a 对象的指针传递给 B 中的 bar 函数,这样我就可以修改并在 bar 中访问 a 的字段。奇怪的是,当我通过另一个类的 A 实例调用 foo 时,我得到了这些错误:

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>  main.cpp
1>d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A'
1>  B.cpp
1>d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C3646: 'b': unknown override specifier
1>d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>  A.cpp
1>d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A'
1>d:\stuff\visual studio 2015\projects\test\test\a.cpp(5): error C2660: 'B::bar': function does not take 1 arguments
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果我不在 B.h 中包含 A.h 并且我不向 bar 函数传递任何内容,则代码可以正常工作。

我尝试用谷歌搜索可能导致这些错误的原因,但我无法自行解决问题,因为我不明白是什么导致了这些错误。我做错了什么?

最佳答案

A.h的头文件中,你有:

#include "B.h"

B.h的头文件中,你有:

#include "A.h"

您有一个循环包含,其中 AB 的定义相互依赖。


一个简单的解决方案是使用 forward declarationB 类 的定义中:

  1. 将文件 B.h 中的 #include "A.h" 行替换为 class B;
  2. B.cpp
  3. 开头添加#include "A.h"

但是请注意,您不能对 class A 使用前向声明,原因是您有 class B 的值 b 作为 a A类的成员变量:

#pragma once
#include "B.h"
class A
{
public:
    A();
    void foo();
    void sayHello();
    B b;             /// I mean this line here more specifically
 };

编译器需要知道 class B 的定义才能确定 class A 的正确大小。这就是为什么你必须将 #include "B.h"A.h 的开头。

关于C++:将 this 指针传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345473/

相关文章:

python - 有没有办法知道函数中传递了多少个参数?

c++ - 用于 Motif 库(C)的水平绘制的 RowColumn 类?

C++ 任何十六进制到字符

pointers - 如何使用BLAS函数传递指向子数组的指针?

c++ - 返回引用和返回值

c - 有没有办法预定义 C 函数参数的预期长度?

c++ - 为什么GRPC AsyncClient在等待完成队列中的下一个结果时抛出Segfault

C++ 类型转换替代虚拟方法

c - 使用指针指向c中的某一行

sql - 如何传递参数进行查询?