错误C2679:二进制' [':找不到采用右手操作数的运算符

时间:2017-11-30 19:11:08

标签: c++ c++11 visual-c++

我正在尝试为我的类项目运行此代码,但我一直在Hex.cpp文件的第20行上得到C2679错误。完整错误是二进制' [':找不到运算符,它采用类型' std :: _ Vector_iterator>>' (或者没有可接受的转换)。我在第154行遇到了另一个错误,但我想事先解决第一个错误。如果有人能帮我解决这个问题,我会很感激。

#include "Hex.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>

using namespace std;

void Hex::HexToBin(string hex) {
    {
        //vector<char> binnum;
        char hexdec[100];
        string h = hex;
        strcpy_s(hexdec, h.c_str());
        vector<char> word(hexdec, hexdec + sizeof hexdec / sizeof hexdec[0]);
        vector<char>::iterator i;
        i = word.begin();
        while (i != word.end())
        {
            switch (word[i]) //error here
            {
            case '0': cout << "0000";
                binum.push_back((0));
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(0);
                break;
            case '1': cout << "0001";
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(1);
                break;
            case '2': cout << "0010";
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(0);
                break;
            case '3': cout << "0011";
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(1);
                break;
            case '4': cout << "0100";
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(0);
                break;
            case '5': cout << "0101";
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                break;
            case '6': cout << "0110";
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                break;
            case '7': cout << "0111";
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                break;
            case '8': cout << "1000";
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(0);
                break;
            case '9': cout << "1001";
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(0);
                binum.push_back(1);
                break;
            case 'A': cout << "1010";
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(0);
                break;
            case 'B': cout << "1011";
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(1);
                break;
            case 'C': cout << "1100";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(0);
                break;
            case 'D': cout << "1101";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                break;
            case 'E': cout << "1110";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                break;
            case 'F': cout << "1111";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                break;
            case 'a': cout << "1010";
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(0);
                break;
            case 'b': cout << "1011";
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                binum.push_back(1);
                break;
            case 'c': cout << "1100";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(0);
                break;
            case 'd': cout << "1101";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                binum.push_back(1);
                break;
            case 'e': cout << "1110";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(0);
                break;
            case 'f': cout << "1111";
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                binum.push_back(1);
                break;
            default: cout << "\nInvalid hexadecimal digit " << hexdec[i]; //error here
            }
            i++;
        }
    }
}

#include <iostream>
#include <string>
#include <vector>
//takes a string and stores, accept a string, convert to binary
/***************************************************************
Class Hex
This class will take a hexidecimal and convert it to a binary
number which will then be stored to be used in other class files.
***************************************************************/
using namespace std;

class Hex {

public:
    void HexToBin(string s);
private:
    vector<char> binum;
    vector<char> word;
    vector<int>::const_iterator i;
};
#include <iostream>
#include "Hex.h"
using namespace std;

int main()
{
    // VARIABLE DECLARATIONS
    Hex hex;
    hex.HexToBin("A24B");
        return 0;
}

1 个答案:

答案 0 :(得分:1)

$row = mysqli_fetch_array($result,MYSQLI_ASSOC); 是一个迭代器,而不是索引。您可以通过取消引用i来获取迭代器指向的元素。

所以你需要改变你的:

*i

为:

        switch (word[i]) //error here

并且:

        switch (*i)

您可以使用单独的计数器变量,也可以只打印 default: cout << "\nInvalid hexadecimal digit " << hexdec[i]; //error here

*i