【问题标题】:How to pass an enum class which is inside another class's function by reference?如何通过引用传递另一个类的函数内部的枚举类?
【发布时间】:2021-11-16 16:28:12
【问题描述】:

我对 C++ 还是很陌生,很抱歉,如果代码有点业余,那可能是问题的根源。

我花了一些时间搜索这个网站和整个网络。有很多使用enums(不是enum class)以及在类之外使用enums 和enums 类的示例,但并没有真正找到对我的特定场景有用的任何东西,见下文。当然,我可能已经看到了答案,但是我的 C++ 还不够先进,还不能识别它。

基本上我想将 2 个“状态”enums 类传递给一个方法,该方法与定义枚举类的类不同,然后根据值更改第二个枚举类的状态在第一个枚举类中。这些枚举类是在一个类中定义的,该类包含一个指向定义该方法的第二个类的指针。类声明位于头文件中,并在包含相关头文件的单独 .cpp 文件中定义。请参阅下面的详细代码和错误。

希望有人可以帮助我解释最后的错误消息并弄清楚如何实现这一点。

main.cpp

#include <iostream>
#include "firstfile.h"

int main() {
    FirstFile firstobject;
    firstobject.Function1();
}

基本上,我在下面的课程中有 2 个enum classes。

firstfile.h

#include "secondfile.h"

class FirstFile 
{
protected:
    SecondFile secondobject;

public:
    enum class enum1 {
        Option1,
        Option2
    } enumIn = enum1::Option1;

    enum class enum2 {
        Option3,
        Option4
    } enumInOut = enum2::Option3;
    // Methods
protected:
public:
    void Function1();
};

firstfile.cpp

#include <iostream>
#include "firstfile.h"

void FirstFile::Function1()
{
    std::cout << "Before the function call enumIn = Option 1 and enumInOut = Option3 " << std::endl;
    secondobject.function2(enumIn, enumInOut);

    if (enumInOut == enum2::Option4) {
        std::cout << "After the function call enumInOut = Option 4" << std::endl;
    }
    else if (enumInOut == enum2::Option3) {
        std::cout << "After the function call enumInOut = Option 3" << std::endl;
    }
    else {
        std::cout << "enumInOut didn't match either Option 3 or Option 4" << std::endl;
    }
}

大部分错误发生的头文件。

secondfile.h

#include "firstfile.h"

class SecondFile 
{   
public:
    void function2(const enum1& enumIn, enum2& enumInOut);
};

最后,这是 class2 中被调用的方法,它应该根据 enum1 的值更新 enum2。

secondfile.cpp

#include <iostream>
#include "firstfile.h"
#include "secondfile.h"

void SecondFile::function2(const enum1& enumIn, enum2& enumInOut) 
{
    if (enumIn == FirstFile::enum1::Option1) {
        enumInOut = FirstFile::enum2::Option4;
    }
}

错误是:

firstfile.cpp
\secondfile.h(16, 28) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.h(16, 28) : error C2143 : syntax error : missing ',' before '&'
\firstfile.cpp(8, 42) : error C2660 : 'SecondFile::function2' : function does not take 2 arguments
\secondfile.h(16, 7) : message: see declaration of 'SecondFile::function2'

Main.cpp
\secondfile.h(16, 28) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.h(16, 28) : error C2143 : syntax error : missing ',' before '&'

secondfile.cpp
\secondfile.h(16, 28) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.h(16, 28) : error C2143 : syntax error : missing ',' before '&'
\secondfile.cpp(5, 39) : error C4430 : missing type specifier - int assumed.Note : C++ does not support default - int
\secondfile.cpp(5, 39) : error C2143 : syntax error : missing ',' before '&'

\secondfile.cpp(6, 6) : error C2065 : 'enumIn' : undeclared identifier
\secondfile.cpp(7, 3) : error C2065 : 'enumInOut' : undeclared identifier

希望这一切都有意义,并期待任何见解来帮助了解错误的原因以及我可以如何解决这些错误。 可能与范围有关,但希望能从中吸取教训。

【问题讨论】:

    标签: c++ class oop enums member


    【解决方案1】:

    你有两个问题:

    • 您有 circular dependencies,因为您将每个类的标头添加到彼此的标头文件中。您可以通过指针成员解决它。将FirstFile 类中的类成员secondobject 作为指针,并在标题中(即在firstfile.h 中)为SecondFile 提供前向声明。

    • 其次,您需要指定 enum1enum2 来自哪个类/范围。您可以为此使用using specifier,枚举将可用于整个类SecondFile 的范围。

    这意味着,您需要类似:(See Online)

    firstfile.h

    class SecondFile; // forward declaration
    
    class FirstFile 
    {
    protected:
        SecondFile* secondobject{ nullptr }; // or smart pointers
    
    public:
        // ...enums and functions
    };
    

    firstfile.cpp

    #include <iostream>
    #include "secondfile.h"
    
    void FirstFile::Function1() 
    {
        // ...other code
        if(secondobject)
        secondobject->function2(enumIn, enumInOut);
        //      ^^^^^^^^^^^^
    
    }
    

    secondfile.h

    #include "firstfile.h"
    
    class SecondFile 
    {    
    public:
        using enum1 = FirstFile::enum1;  // specify from where the enums are
        using enum2 = FirstFile::enum2;
        void function2(const enum1& enumIn, enum2& enumInOut);
         
        // ...other codes
    };
    

    secondfile.cpp

    #include "secondfile.h"
    
    void SecondFile::function2(const enum1& enumIn, enum2& enumInOut) 
    {
        if (enumIn == enum1::Option1) {
            enumInOut = enum2::Option4;
        }
    }
    

    【讨论】:

    • 您好,感谢您的快速回复和全面的答复。看起来有几个基本错误(循环依赖)和缺少前向声明。认为枚举的范围是问题的一部分,很高兴我是对的。您提供的代码有效,但由于从未创建过 secondobject,因此永远不会调用该函数。尝试创建它,但在没有获得指向类的指针时出现错误。您可以将该行添加到代码中吗?将花一些时间把它拆开来了解它是如何工作的。再次感谢。
    • 谢谢,这正是我将代码用于创建第二个对象的地方,但我没有使用 Curley 大括号,不确定我以前是否见过。这可能是我得到指针转换错误的原因。另外,为什么前向声明解决了我的范围界定问题,是否与 2 个文件的不同翻译单元有关?感谢您的帮助。
    • 感谢您的帮助和各种链接,非常有用。我接受了你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多