简体   繁体   English

为什么在前向声明后出现未定义的类型错误?

[英]Why am I getting undefined type error after a forward-declaration?

I'm using this code in C++ CLI. 我在C ++ CLI中使用此代码。 However this shouldn't make any difference from C++. 但是,这与C ++应该没有什么区别。
I'm looking for a solution to get rid of that error. 我正在寻找解决方案来摆脱这个错误。

Code : 代码:

ref class B;
ref class A;

public ref class A
{
public:
    A() {}
    B^ b;
    void HelloFromA(){
        b->HelloFromB();
    }
};

public ref class B
{
public :
    A^ a;
    B() {}
    void HelloFromB(){
        a->HelloFromA();
    }
};

You need to move the bodies of the functions that invoke member functions on the forward-declared classes outside of the headers, to places where definitions are available: 您需要将在标头之外的,在向前声明的类上调用成员函数的函数的主体移到可以使用定义的地方:

void A::HelloFromA(){
    b->HelloFromB();
}

Otherwise, the compiler knows that B is available, but it does not know that B has the HelloFromB member function that takes no arguments. 否则,编译器知道B可用,但它不知道B具有不带参数的HelloFromB成员函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++中的前向声明 - forward-declaration in c++ 为什么我得到没有类型错误的声明 - 链表? - Why am I getting declaration with no type error - linked list? 另一个函数内部的函数前向声明 - function forward-declaration inside another function 未定义类型错误,即使具有前向声明 - Undefined type error even with forward declaration QT C++ 前向声明问题? - QT C++ forward-declaration problem? 我可以省略类的前向声明的非公共继承吗? - Can I omit non-public inheritance for forward-declaration of classes? 为什么我收到“标识符未定义”错误? - Why am I getting an “identifier undefined” error? 为什么我在声明本身中收到“未在范围内声明”错误? - Why am I getting a "not declared in scope" error in the declaration itself? 当包含标题时,为什么在代码中出现“使用未定义类型”错误? - Why am I getting an “use of undefined type” error in my code, when I have the header for it included? 在成员函数中,我得到错误“未定义类型'struct(name)'的无效使用-'struct(name)'的正向声明” - In member function I get the error “ invalid use of undefined type 'struct (name)' - forward declaration of 'struct (name)' ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM