简体   繁体   English

枚举数组作为参数c ++

[英]Enum Array as parameter c++

I am new to c++ and I want to include an array of Enum values, and I am getting a syntax error in Microsoft Visual Studio. 我是C ++的新手,我想包含一个Enum值数组,并且在Microsoft Visual Studio中遇到语法错误。 I am not sure why this is the case any help much appreciated. 我不确定为什么会有这样的帮助。 The error number is C2061 and it states "syntax error: identifier 'VerboseBinary'. Here is the code: 错误号为C2061,它指出“语法错误:标识符'VerboseBinary'。这是代码:

Header file verbose_binary.h 头文件verbose_binary.h

    #pragma once
    #include <bitset>
    enum class VerboseBinary : int {
        One,
        Two,
        Four,
        Eight,
        Sixteen,
        Null,
    };

    void convert(std::bitset<5> bs, VerboseBinary aVB[6]);

verbose_binary.cpp verbose_binary.cpp

        #include "verbose_binary.h"
        #include "stdafx.h"
        #include <bitset>
        #include <string>
        #include <iostream>


        void convert(std::bitset<5> bs, VerboseBinary aVB[6]) {
            VerboseBinary VBArray[6] = {
                VerboseBinary:: One,
                VerboseBinary:: Two,
                VerboseBinary:: Four,
                VerboseBinary:: Eight,
                VerboseBinary:: Sixteen,
                VerboseBinary:: Null
            };

            for (int i = 0; i < 5; i++) {
                if (bs.test(i)) {
                    aVB[i] = VBArray[i];
                }
                else {
                    aVB[i] = VerboseBinary::Null;
                }

            }
            aVB[5] = VerboseBinary::Null;

        }

Main 主要

#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <bitset>

#include "verbose_binary.h"

int main() {
    int a, b;
    std::bitset<5> aBs, bBs;
    std::cout << "Enter two numbers between 0-31:" << std::endl;
    std::cin >> a >> b;
    if (a<0 || a>31) return -1;
    if (b<0 || b>31) return -2;
    aBs = static_cast<std::bitset<5>>(a);
    bBs = static_cast<std::bitset<5>>(b);
    // std::cout << aBs << " " << bBs << std::endl;
    VerboseBinary aVB[6];
    VerboseBinary bVB[6];
    convert(aBs, aVB);
    convert(bBs, bVB);
    return 0;
}

Lol, so it looks like one of these issues was resposible for the error: 大声笑,所以看来这些问题之一是可以解决该错误的:

  • What version of Visual Studio are you using? 您正在使用哪个版本的Visual Studio? enum with class became available with VS 2012. VS 2012中提供了带有类的枚举。

  • Also, there is a stray comma at the end of your enum's definition. 另外,枚举定义的末尾会有一个逗号。

  • Also, stdafx.h should appear before any other includes in verbose_binary.cpp. 另外,stdafx.h应该出现在verbose_binary.cpp中的任何其他包含项之前。

  • Main has a benign double-inclusion for <iostream> Main对于<iostream>具有良性双包含

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM