"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?
我想在向量中存储字符。但是当我想推回匹配字符时,它不起作用。
此行中显示错误 -
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
.看看这里。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- visual c++,如何获取解决方案目录中的代码
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 为什么这段代码不起作用,我该如何解决?