"error: no matching function for call to ‘push_back(char [6])" .如何解决此代码中的错误?

"error: no matching function for call to ‘push_back(char [6])". How to solve the error in this code?

本文关键字:何解决 解决 错误 代码 char matching function no error for call      更新时间:2023-10-16

我想在向量中存储字符。但是当我想推回匹配字符时,它不起作用。

此行中显示错误 -

v.push_back(arithmetic_operator[i]);

整个代码是——

#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<char>v;
    char str[100];
    gets(str);
    char *ptr;
    ptr = strtok(str, " ");
    char arithmetic_operator[6][6] = {"+", "-", "*", "/", "%", "="};
    while(ptr !=NULL)
    {   
        // arithmetic operator
        for(int i=0; i<sizeof(arithmetic_operator); i++){
            if(strcmp(arithmetic_operator[i], ptr) == 0)
            {
                v.push_back(arithmetic_operator[i]);
            }
            else
            {
                continue;
            }
        }
        ptr = strtok(NULL, " ");
    }
    for (auto it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    return 0;
}

当输入为 a = b + c 时,预期输出将为 =,+

#include<bits/stdc++.h>

只是没有。从不。甚至一次都没有。不要这样做,并极其谨慎地对待任何显示这一点的示例代码(理想情况下,忽略它并找到不是垃圾的东西(。

vector<char>v;
v.push_back(...);

push_back的文档显示,vector<char>需要字符作为参数。

char arithmetic_operator[6][6] = {"+", ... };

但显然arithmetic_operator[i]一点都不像炭。如果您打印整个错误,它甚至可能会告诉您类型到底是什么,但作为线索,arithmetic_operator[i][0]是一个char

回答您的问题:您正在将char*传递给vector<char>。因此,将v定义为vector<char*>将解决错误。

此外,如前所述,gets在 C++11 中已弃用,并将在 C++14 中删除。对于相同的功能,您可以使用 std::fgets .看看这里。