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

Posted

技术标签:

【中文标题】错误 C2679:二进制“[”:未找到采用右手操作数的运算符【英文标题】:error C2679: binary '[': no operator found which takes a right-hand operand 【发布时间】:2017-11-30 19:11:07 【问题描述】:

我正在尝试为我的班级项目运行此代码,但我不断在 Hex.cpp 文件的第 20 行收到 C2679 错误。完整的错误是二进制 '[': no operator found 它采用 '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;

【问题讨论】:

请提供minimal reproducible example i 不是 size_type (std::vector::operator[] 要求)它是一个迭代器。为什么不使用range based loop? 也许这有帮助? ***.com/questions/7396289/vector-iterators std::string 具有开始/结束迭代器和运算符 []。使用它。 有更简单的方法来做你想做的事,而不需要大开关。 【参考方案1】:

i 是一个迭代器,而不是索引。您可以通过像*i 这样的取消引用来获取迭代器指向的元素。

所以你需要改变你的:

        switch (word[i]) //error here

到:

        switch (*i)

对于:

        default: cout << "\nInvalid hexadecimal digit " << hexdec[i]; //error here

您可以使用单独的计数器变量或只打印*i

        default: cout << "\nInvalid hexadecimal digit " << *i;

【讨论】:

看起来这解决了这个问题。谢谢! 我看到我得到的 Invalid hexadecimal digit 比我预期的要多。您只需要复制字符串中的字符数,而不是缓冲区。尝试初始化vector&lt;char&gt; word(begin(hex), end(hex));,您可以删除hexdech。这是我的尝试ideone.com/e.js/6vq2VV

以上是关于错误 C2679:二进制“[”:未找到采用右手操作数的运算符的主要内容,如果未能解决你的问题,请参考以下文章

错误:二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符 [重复]

使用 std::copy - 错误 C2679:找不到正确的二进制 '=' 运算符

使用 QDataStream 序列化自定义类导致 C2679 错误

未找到用于架构 armv7 存档错误 xcode 5.1.1 的金属框架

Xcode 7 中未找到解析 UI 错误

Angular 构建 - 未找到模块:错误:无法解析“控制台”