如何通过引用传递另一个类的函数内的枚举类?

玩家

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

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

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

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

main.cpp

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

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

基本上,我enum classe在以下课程中有 2秒。

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

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

济州

你有两个问题:

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

  • 其次,你需要指定enum1,并enum2从中类/范围是。您可以为此使用说明using,并且枚举将可用于整个类SecondFile的范围。

这意味着,你需要这样的:在线看

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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过引用另一个类的构造函数传递对象时出错

从另一个类的函数调用std :: async时,如何传递另一个类的函数?

C ++将成员函数引用从一个类实例传递到另一个类实例

如何从另一个类的Builder中引用枚举字段?

通过引用const函数来调用另一个类的非const函数

如何通过另一个类/通过在WPF中的函数中传递按钮来更新按钮的颜色

SwiftUI通过引用将类对象传递给另一个视图

C#如何使类变量引用类内的另一个值

如何声明另一个类中的枚举?

Python如何将类函数从实例传递到另一个类

在Kivy中引用另一个类的函数

如何从python中的另一个外部文件引用python类中函数内的变量?

如何将一个类的函数作为另一个类的另一个函数的参数传递

通过方法从另一个类引用一个类变量

如何通过Java中的方法将对象从一个类传递到另一个类

通过另一个类的方法传递要访问的一个类的成员

通过另一个类将对象传递给一个类

PHP将一个类传递给另一个类构造函数

使用构造函数将一个类传递给另一个类

如何获取listView位置值并通过intent传递给另一个类

如何在C ++中将类对象的引用正确传递到另一个类对象

如何将另一个类的函数作为参数传递?

C ++如何将成员函数指针传递给另一个类?

如何从另一个函数传递的单独类中访问DataGrid

如何将编写的对象传递给另一个类的构造函数?

如何从一个类到另一个类引用变量?

如何使用另一个类从一个类中引用属性

是否可以通过使用另一个类的变量在枚举(调用函数)中具有计算属性?

将类引用传递给另一个类不起作用