我对 C++ 还是很陌生,所以很抱歉,如果代码有点业余,那可能是问题的根源。
我花了一些时间在这个网站和整个网络上搜索。有很多使用enum
s (not enum class
) 以及在类之外使用enum
s 和enum
s 类的示例,但并没有真正找到对我的特定场景有用的任何东西,请参见下文。当然,我可能已经看到了答案,但是我的 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] 删除。
我来说两句