【发布时间】: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