C++ 使用未定义类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18284678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:50:50  来源:igfitidea点击:

Use of undefined type

c++

提问by Ben

#include <iostream>

class Core;
class State;

int main (){
        std::cin.get();
        return 0;
}

class State{
public:
    State(Core* core){
        core->setState();
    }
};

class Core{
public:
    Core(){
        State state(this);
    }
    void setState(){
        std::cout << "setting state" << std::endl;
    }
};

I keep getting the "use of undefined type" error. I thought that if I forward declare both of the classes, it would fix the problem but I can't figure it out. Is it just silly c++ syntax that I'm missing?

我不断收到“使用未定义类型”错误。我认为如果我转发声明这两个类,它会解决问题,但我无法弄清楚。我缺少的只是愚蠢的 C++ 语法吗?

EDIT: Sorry about the gamestate typo, I've changed it to State and it still produces the error.

编辑:对不起,游戏状态错字,我已将其更改为状态,但它仍然会产生错误。

回答by Chad

In State::State, you are using Corebefore it is actually defined. You can fix this easily in your example by moving the constructor implementation out of the class definition:

在 中State::State,您Core在实际定义之前使用。通过将构造函数实现移出类定义,您可以在示例中轻松解决此问题:

class State{
public:
    State(Core* core);
};

class Core{
   // This stays the same...
};

State::State(Core* core)
{
   core->setState();
}

It's much more common in practice to have the implementation of these functions in a separate implementation (.cpp) files, in which case the forward declarations would work as you've expected.

在实践中更常见的是在单独的实现 ( .cpp) 文件中实现这些函数,在这种情况下,前向声明将按您的预期工作。

In that case:

在这种情况下:

// State.h
class Core;

class State{
public:
    State(Core* core);
};

And

// Core.h
#include "State.h"
#include <iostream> //This is probably a good reason to further separate
                    //Core.h into Core.h and Core.cpp

class Core{
public:
    Core(){
        State state(this);
    }

    void setState(){
        std::cout << "setting state" << std::endl;
    }
};

And the implementation file:

和实现文件:

// State.cpp
#include "State.h"
#include "Core.h"

State::State(Core* core)
{
   core->setState();
}

回答by Dietmar Kühl

You can forward declare a type when you only need to name, e.g., to form pointers, references, function value arguments, or return types. If you substantially use it, e.g., in a function definition or by dereferencing a pointer, you need the definition. The way to deal with the problem about is to declare the member function but not to define it in the class definition of State. Instead, you'd define it once the definition of Corewas seen:

当您只需要命名时,您可以转发声明类型,例如,形成指针、引用、函数值参数或返回类型。如果您大量使用它,例如,在函数定义中或通过取消引用指针,您需要定义。解决这个问题的方法是声明成员函数而不是在 的类定义中定义它State。相反,一旦Core看到了 的定义,您就可以定义它:

State::State(Core* core){
    core->setState();
}