C ++ 11枚举头文件 [英] C++11 enums in a header file

查看:352
本文介绍了C ++ 11枚举头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后编辑:我是最新的人物



我抬头看看,在Qt-Creator中你必须指定你想在配置文件中包含C ++ 11 .pro文件。我这样做,一切都很好。我很抱歉,



很明显,我在Google上很糟糕,或者我错过了我非常想要的建议。



我一直在尝试在标题中放入一个范围的枚举,使其可用于我当前项目中的其他文件。

  // Drawing.h 
枚举类图{Line,Square,Triangle};

//action.cpp
#includeDrawing.h

绘制analyzeStuff(VertexArray顶点){
绘制theDrawing; //这可以。
if(...)
theDrawing = Drawing :: Line; //这会引发错误。
//...其他else-ifs使用相同的东西。
return theDrawing; //这也没关系
}

编辑:提到我在这个标题中有一个第二课很重要文件?我怀疑,但这真的是这个代码与我自己之间唯一的区别。

解决方案

不,这里没有问题。



确保你真的编译为C ++ 11,编译器支持范围枚举。



请注意,C ++不太在乎头文件。您可以将头文件内容复制并粘贴到源文件(替换 #include 指令)中,以生成工作测试用例。


FINAL EDIT: I AM THE WORST PERSON

I looked up and apparently in Qt-Creator you have to specify you want to include C++11 in the config .pro file. I did that, everything works fine. I apologize.

So clearly either I'm terrible at Googling or I'm missing the advice I so desperately want.

I've been trying to put in a scoped enum in a header to make it available to other files in my current project.

//Drawing.h
enum class Drawing {Line, Square, Triangle};

//action.cpp
#include "Drawing.h"

Drawing analyzeStuff(VertexArray vertices){
    Drawing theDrawing; //This is fine. 
    if(...)
        theDrawing = Drawing::Line; //This brings up an error.
    //...Other else-ifs using the same thing.
    return theDrawing; //This is also fine.
}

EDIT: Is it important to mention that I have a second class within this header file? I doubt it, but that's really the only variation between this code and my own.

解决方案

Nope, there's no problem here.

Make sure you're really compiling as C++11, with a compiler that has support for scoped enums.

Note that C++ doesn't really care about header files. You can literally copy-and-paste your header file contents into the source file (replacing the #include directive) to produce your working testcase.

这篇关于C ++ 11枚举头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆